Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Using Sentry To Track Django live Events
In the previous tutorial you saw how to setup sentry, let us now learn how to track exceptions and events in SENTRY. We will setup client and server end tracking for Django Project. Note: My domain is set as "http://sentry.domain.com". Now after first login the screen should be similar to this. As per Sentry's Internal Architecture, Team will have access to projects. Create a team and assign a project to team, every member in team will now have access to it. As per your convineance. any combination of members can grouped as teams (which can be managed in Team Settings option in Dashboard page). Creating Team and Project. Click on Create Team button in Dashboard. decide a Team Name and save changes create a Project. In Project DashBoard> Settings > Client Keys, you can find DSN (reffered in this tutorial as dsn) and DSN (Public) - (Reffered in this tutorial as public-dsn) Modifications for Django Application. The recieving end is sentry but error tracker and logger is RAVEN, Install raven in Django Project environment pip install raven open settings.py include the below lines in settings.py RAVEN_CONFIG = { 'dsn': '<your-dsn-here>', } LOGGING = { 'version': 1, 'disable_existing_loggers': True, 'root': { 'level': 'WARNING', 'handlers': ['sentry'], }, 'formatters': { 'verbose': { … -
Debugging Django Management Commands in PyCharm
Photo by Jill Heyer My favorite editor for Python projects is PyCharm. Besides editing code, it allows you to inspect the database, work with Git repositories, run management commands, execute bash commands and Python scripts, and debug code just in the same window. In this article, I will show you how to set breakpoints and debug Django management commands visually in PyCharm. Django management commands are scripts that can be executed on your Django project to do something with the project database, media files, or code. Django itself comes with a bunch of commands like: migrate, runserver, collectstatic, makemessages, and clearsessions. Management commands can be executed like this: (myproject_env)$ python manage.py clearsessions If you want to create a custom management command in your project, you can find how to do that in the official Django documentation. Also you can find some practical examples in the Chapter 9, Data Import and Export of the Web Development with Django Cookbook - Second Edition. In this example, I won't create any new management command, but will debug the clearsessions command that is coming from Django and is located at django/contrib/sessions/management/commands/clearsessions.py. First of all, let's click on "Edit Configurations..." in the top toolbar just … -
Django meetup Amsterdam 18 May 2016
Summary of the Django meetup organized at crunchr in Amsterdam, the Netherlands. (I gave a talk on the django admin, which I of course don't have a summary of, yet, though my brother made a summary of an almost-identical talk I did the friday before) Reducing boilerplate with class-based views - Priy Werry A view can be more than just a function. They can also be class based, django has quite a lot of them. For example the TemplateView that is very quick for rendering a template. Boilerplate reduction. Django REST framework is a good example of class based views usage. It really helps you to reduce the number of boring boilerplate and concentrate on your actual code. Examples of possible boilerplate code: Parameter validation. Pagination. Ordering. Serialisation. They wanted to handle this a bit like django's middleware mechanism, but then view-specific. So they wrote a base class that performed most of the boilerplate steps. So the actual views could be fairly simple. It also helps with unit testing: normally you'd have to test all the corner cases in all your views, now you only have to test your base class for that. Custom base classes also often means you … -
Evennia 0.6 !
As of today, I merged the development branch to make version 0.6 of the MU* development system and server Evennia. Evennia 0.6 comes with a lot of updates, mainly in the way Evennia talks to the outside world. All communication is now standardized, so there are no particular treatment of things like text - text is just one of any standardized commands being passed between the server the client (whether over telnet, ssh, websockets or ajax/comet). For example the user can now easily plug in "inputfuncs" to handle any data coming from the client. If you want your client to offer some particular functionality, you just need to plop in a python function to handle it, server-side. We also now offer a lot of utility functions for things like monitoring change (tell the client whenever your health status changes so it can update a health bar or flash the screen).The HTML5 webclient has itself updated considerably. Most is happening behind the scenes though. Notably the webclient's javascript component is split into two: evennia.js, acts as a library for handling all communication with the server part of Evennia. It offers events for a gui library to plug into and send/receive. It will … -
Ports and Adapters in python - part one
First part of series about Django application made using Ports and Adapters design pattern. -
Ports and Adapters in python - part one
Welcome! Today I'm going to start series about how to use port and adapter design pattern in simple django application. Let me explain a little bit what exactly ports and adapters design pattern is. According to this article (which by the way I strongly recommend to read) it is a way to separate business logic from user code. What I mean by that? Let pretend that you want to create simple django application which connects to reddit using its API. Then app retrieves the content of search query provided by the user. After that user can save for later founded link. In this blog post, I will focus only on reddit API part. Normally you will write some module using request for retrieving search results from reddit. But what when it comes to testing such code? You just mock requests calls or use responses library. How do you do it in ports and adapters way? You will have one thing called port for all external connections. Throught this all requests to external APIs will be done because who knows if the reddit will not change to duckduckgo? In such case you just add DuckDuckGo Adapter and you are all set. … -
Ports and Adapters in python - part one
Welcome! Today I'm going to start series about how to use port and adapter design pattern in simple django application. Let me explain a little bit what exactly ports and adapters design pattern is. According to this article (which by the way I strongly recommend to read) it is a way to separate business logic from user code. What I mean by that? Let pretend that you want to create simple django application which connects to reddit using its API. Then app retrieves the content of search query provided by the user. After that user can save for later founded link. In this blog post, I will focus only on reddit API part. Normally you will write some module using request for retrieving search results from reddit. But what when it comes to testing such code? You just mock requests calls or use responses library. How do you do it in ports and adapters way? You will have one thing called port for all external connections. Throught this all requests to external APIs will be done because who knows if the reddit will not change to duckduckgo? In such case you just add DuckDuckGo Adapter and you are all set. … -
Deploying a Django Website on Heroku
Photo by Frances Gunn Once you have a working project, you have to host it somewhere. One of the most popular deployment platforms nowadays is Heroku. Heroku belongs to a Platform as a Service (PaaS) category of cloud computing services. Every Django project you host on Heroku is running inside a smart container in a fully managed runtime environment. Your project can scale horizontally (adding more computing machines) and you pay for what you use starting with a free tier. Moreover, you won't need much of system administrator's skills to do the deployment - once you do the initial setup, the further deployment is as simple as pushing Git repository to a special heroku remote. However, there are some gotchas to know before choosing Heroku for your Django project: One uses PostgreSQL database with your project. MySQL is not an option.You cannot store your static and media files on Heroku. One should use Amazon S3 or some other storage for that.There is no mailing server associated with Heroku. One can use third-party SendGrid plugin with additional costs, GMail SMTP server with sent email amount limitations, or some other SMTP server.The Django project must be version-controlled under Git.Heroku works with Python … -
Ratchets & Levers
There are a couple of metaphors that tend to guide my thinking about the practice of security: ratchets and levers. Ratchets Dr. Schorsch, CC-BY-SA 3.0, via Wikimedia Commons A ratchet is a kind of one-way gear, with angled teeth and a pawl that allows motion in one direction only. In the physical world we use ratchets to help lift or move heavy loads. Using a ratchet, we can overcome the massive inertia of a heavy object by breaking the movement down into small, easy, irreversible steps. -
Conditional Python Dependencies
Since the inception of Python wheels that install without executing arbitrary code, we needed a way to encode conditional dependencies for our packages. Thanks to PEP 426 and PEP 508 we do have a blessed way but sadly the prevalence of old setuptools versions makes them a minefield to use. -
Django Channels for Background Tasks
Django Channels is the most exciting thing to happen to Django since well Django :). This little tutorial is what you need to add a background task processor to Django using channels. Our task for this example will just be outputting "Hello, Channels!", but you could image running a subprocess on some data or sending an email. NOTE: channel works on an at-least-once delivery model, so it is possible a message in a channel could be lost, delivery isn't guaranteed. That also means consumers don't have to worry about duplicates. This example will be stripped down to the basic code without much error checking. There are detailed examples one here and here. We will start with a simple Django 1.9 app without channels # urls.py from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.home, name='home'), ] # views.py from django.shortcuts import render def home(request, template="home.html"): print("Hello, Channels!") # long running task of printing. return render( request, template, dict(), ) You will need to define a home.html template where Django can find it, but besides that this simple site should work and synchronously render what is in your home.html and output "Hello, Channels!" on your terminal. Now … -
How to migrate your existing Django project to Heroku
Recently I had some fun with Heroku, the well known PaaS provider. I had a small personal Django project I use for invoicing that I ran locally with ./manage.py runserver when needed. That was a perfect candidate for the Heroku free plan because I need to access the app only occasionally. In this tutorial I assume you have a basic knowledge of what Heroku is, and that you already know how to create and deploy a Python project on Heroku. In case you miss some basic information you can refer to the good Getting started tutorial on Heroku with Python. Here I focus on my use case, which was to migrate an existing Django project on Heroku platform. My existing Django project was structured in accordance to the best practices I read in the wonderful Two Scoops of Django book, so my project structure was similar to this: django/ ├── project │ ├── __init__.py │ ├── settings │ │ ├── __init__.py │ │ ├── base.py │ │ ├── local.py │ │ └── production.py │ ├── urls.py │ ├── wsgi.py ├── app │ ├── __init__.py │ ├── admin.py │ ├── models.py │ ├── tests.py │ ├── urls.py │ ├── views.py └── … -
How to migrate your existing Django project to Heroku
Recently I had some fun with Heroku, the well known PaaS provider. I had a small personal Django project I use for invoicing that I ran locally with ./manage.py runserver when needed. That was a perfect candidate for the Heroku free plan because I need to access the app only occasionally. In this tutorial I assume you have a basic knowledge of what Heroku is, and that you already know how to create and deploy a Python project on Heroku. In case you miss some basic information you can refer to the good Getting started tutorial on Heroku with Python. Table of Contents How to structure your Django project for Heroku How to configure your Django project for Heroku Create an Heroku application for your Django project Migrating data Media files on AWS S3 How to structure your Django project for Heroku Here I focus on my use case, which was to migrate an existing Django project on Heroku platform. My existing Django project was structured in accordance to the best practices I read in the wonderful Two Scoops of Django book, so my project structure was similar to this: django/ ├── project │ ├── __init__.py │ ├── settings │ … -
Integration Of GitHub API with python django
Using Github integration by Django, we can get the user verified email id, general information, git hub URL, id, disk usage, public, private repo's, gists and followers, following in a less span of time. These Following steps are needed for Github integration: 1. creating git hub app 2. Authenticating user and getting an access token. 3. Get user information, work history using access token. 1. Creating Github App a. To create an app, click on create an application on top of a page. Here you can give application name then the application will be created. b. Now you can get the client id, secret of an application and you can give redirect urls of your applications. 2. Authenticating user and getting an access token. a. Here We have to create a GET request for asking user permission. POST "https://github.com/login/oauth authorize?client_id=GIT_APP_ID&redirect_uri=REDIRECT_URL&scope=user,user:email&state=dia123456789ramya" GIT_APP_ID: your application client id, SCOPE: List of permissions to request from the person using your app REDIRECT_URI: The url which you want … -
Dynamically Adding Google Maps with Marker In Django
Google Maps allows you to display maps on your website, we can also customize maps, and the information on maps. The Google Maps API is a JavaScript library. It can be added to a web page with the following script tags: We are creating a div to holds the google map. Here we are also giving an option to search the place on a google map. then add a DOM listener that will execute the getGoogleMap() function on window load (when the page is initially loaded): google.maps.event.addDomListener(window, "load", getGoogleMap) In the above example, we are already loading 3 markers in Bangalore, Chennai, Hyderabad. Then again if a user marks any location, it will display the marker with longitude, latitude of the place(if u want to store) by deleting user previously selecting markers. We can also set the position description dynamically using the info window -
Extract text with OCR for all image types in python using pytesseract
What is OCR? Optical Character Recognition(OCR) is the process of electronically extracting text from images or any documents like PDF and reusing it in a variety of ways such as full text searches. In this blog, we will see, how to use 'Python-tesseract', an OCR tool for python. pytesseract: It will recognize and read the text present in images. It can read all image types - png, jpeg, gif, tiff, bmp etc. It’s widely used to process everything from scanned documents. Installation: $ sudo pip install pytesseract Requirements: * Requires python 2.5 or later versions. * And requires Python Imaging Library(PIL). Usage: From the shell: $ ./pytesseract.py test.png Above command prints the recognized text from image 'test.png'. $ ./pytesseract.py -l eng test-english.jpg Above command recognizes english text. In Python Script: import Image from tesseract import image_to_string print image_to_string(Image.open('test.png')) print image_to_string(Image.open('test-english.jpg'), lang='eng') To Know more about our Django CRM(Customer Relationship Management) Open Source Package. Check Code -
Implement search with Django-haystack and Elasticsearch Part-I
Haystack works as search plugin for django. You can use different back ends Elastic-search, Whose, Sorl, Xapian to search objects. All backends work with same code. In this post i am using elasticsearch as backend. Installation: pip install django-haystack Configuration: add haystack to installed apps INSTALLED_APPS=[ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', #add haystack here 'haystack', 'books' ] Settings: Add back-end settings for haystack. HAYSTACK_CONNECTIONS = { 'default': { 'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine', 'URL': 'http://127.0.0.1:9200/', 'INDEX_NAME': 'haystack_books', }, } Above settings for elastic search. Add signal processor for haystack. This signal will update objects in index. HAYSTACK_SIGNAL_PROCESSOR = … -
Setting Up Coveralls for Django Project
Why coveralls? Coveraslls will check the code coverage for your Django project test cases. To use coveralls.io your code must be hosted on GitHub or BitBucket. install coveralls pip install coveralls Using Travis If you are using Travis for you CI. add below script in .travis.yml file in project root folder language: python # python versions python: - "3.4" - "2.7.4" env: -DJANGO=1.8 DB=sqlite3 # install requirements install: - pip install -r requirements.txt - pip install coveralls # To run tests script: - coverage run --source=my_app1, my_app2 manage.py test # send coverage report to coveralls after_success: coveralls Signup with GitHub in https://coveralls.io/ and activate coveralls for you repo. Thats it. Happy Testing... -
How to Create your own e-commerce shop using Django-Oscar.
Oscar is an open-source ecommerce framework for Django. Django Oscar provides a base platform to build an online shop. Oscar is built as a highly customisable and extendable framework. It supports Pluggable tax calculations, Per-customer pricing, Multi-currency etc. 1. Install Oscar $ pip install django-oscar 2. Then, create a Django project $ django-admin.py startproject <project-name> After creating the project, add all the settings(INSTALLED_APPS, MIDDLEWARE_CLASSES, DATABASES) in your settings file And you can find the reference on how to customize the Django Oscar app, urls, models and views here. Customising/Overridding templates: To override Oscar templates, first you need to update the template configuration settings as below in your setting file. import os location = lambda x: os.path.join( os.path.dirname(os.path.realpath(__file__)), x) TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', 'django.template.loaders.eggs.Loader', ) from oscar import OSCAR_MAIN_TEMPLATE_DIR TEMPLATE_DIRS = ( location('templates'), OSCAR_MAIN_TEMPLATE_DIR, ) Note: In the 'TEMPLATE_DIRS' setting, you have to include your project template directory path first and then comes the Oscar's template folder which you can import from oscar. By customising templates, you can just replacing all the content with your own content or you can only change blocks using "extends" Ex: Overriding Home page {% extends 'oscar/promotions/home.html' %} {% block content %} Content goes here … -
Mark Lavin to Give Keynote at Python Nordeste
Mark Lavin will be giving the keynote address at Python Nordeste this year. Python Nordeste is the largest gathering of the Northeast Python community, which takes place annually in cities of northeastern Brazil. This year’s conference will be held in Teresina, the capital of the Brazilian state of Piauí. -
NGINX for static files for dev python server
When you work on the backend part of django or flask project and there are many static files, sometimes the development server becomes slow. In this case it’s possible to use nginx as reverse proxy to serve static. I’m using nginx in docker and the configuration is quite simple. Put in some directory Dockerfile and default.conf.tmpl. Dockerfile 1 2 3 4 5 FROM nginx:1.9 VOLUME /static COPY default.conf.tmpl /etc/nginx/conf.d/default.conf.tmpl EXPOSE 9000 CMD envsubst '$APP_IP $APP_PORT' < /etc/nginx/conf.d/default.conf.tmpl > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;' default.conf.tmpl 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 server { listen 9000; charset utf-8; location /site_media { alias /static; } location / { proxy_pass http://${APP_IP}:${APP_PORT}; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } Build image with docker build -t dev-nginx . command. To run it: 1 docker run --rm -it -v `pwd`/static:/static -p 9000:9000 -e APP_IP=<your ip from ifconfig> -e APP_PORT=8000 dev-nginx Then you can access your development server though http://<localhost|docker-machine-ip>:9000. -
Using Django's built in signals and writing custom signals.
Django has a beautiful feature of signals which will record all the actions performed on the particular model. In the current blog post, we’ll learn how to use Django's built-in signals and how to create custom signal Using Django’s built in Signals: Django has a lot of built-in signals like pre_save, post_save, pre_delete and post_delete and etc., For more information about Django's built-in signals visit https://docs.djangoproject.com/en/1.9/ref/signals/. Now we’ll learn how to use Django's pre_delete signal with a simple example. In the way we use pre_delete in the present blog post we can use other signals also in the same way. We have two models called Author and Book their models are defined in models.py as below. # In models.py from django.db import models class Author(models.Model): full_name = models.CharField(max_length=100) short_name = models.CharField(max_length=50) class Book(models.Model): title = models.CharField(max_length=100) slug = models.SlugField(max_length=100) content = model.TextField() status = models.CharField(max_length=10, default=”Drafted”) author_id = model.PositiveIntegerField(null=True) In the above two models we are not having an author as foreignKey to Book model, so by default when the Author gets deleted it won’t delete all the Books written by the author. This is the … -
Pygrunn: Micropython, internet of pythonic things - Lars de Ridder
(One of my summaries of the one-day 2016 PyGrunn conference). micropython is a project that wants to bring python to the world of microprocessors. Micropython is a lean and fast implementation of python 3 for microprocessors. It was funded in 2013 on kickstarter. Originally it only ran on a special "pyboard", but it has now been ported to various other microprocessors. Why use micropython? Easy to learn, with powerful features. Native bitwise operations. Ideal for rapid prototyping. (You cannot use cpython, mainly due to RAM usage.) It is not a full python, of course, they had to strip things out. "functools" and "this" are out, for instance. Extra included are libraries for the specific boards. There are lots of memory optimizations. Nothing fancy, most of the tricks are directly from compiler textbooks, but it is nice to see it all implemented in a real project. Some of the supported boards: Pyboard The "BBC micro:bit" which is supplied to 1 million school children! Wipy. More of a professional-grade board. LoPy. a board which supports LoRa, an open network to connect internet-of-things chips. Development: there is one full time developer (funded by the ESA) and two core contributors. It is stable and … -
Pygrunn: Kliko, compute container specification - Gijs Molenaar
(One of my summaries of the one-day 2016 PyGrunn conference). Gijs Molenaar works on processing big data for large radio telescopes ("Meerkat" in the south of Africa and "Lofar" in the Netherlands). The data volumes coming from such telescopes are huge. 4 terabits per seconds, for example. So they do a log of processing and filtering to get that number down. Gijs works on the "imaging and calibration" part of the process. So: scientific software. Which is hard to install and fragile. Especially for scientists. So they use ubuntu's "lauchpad PPA's" to package it all up as debian packages. The new hit nowadays is docker. Containerization. A self-contained light-weight "virtual machine". Someone called it centralized agony: only one person needs to go through the pain of creating the container and all the rest of the world can use it... :-) His line of work is often centered around pipelines. Data flows from one step to the other and on to the next. This is often done with bash scripts. Docker is nice and you can hook up multiple dockers. But... it is all network-centric: a web container plus a database container plus a redis container. It isn't centered on data … -
Pygrunn keynote: the future of programming - Steven Pemberton
(One of my summaries of the one-day 2016 PyGrunn conference). Steven Pemberton (https://en.wikipedia.org/wiki/Steven_Pemberton) is one of the developers of ABC, a predecessor of python. He's a researcher at CWI in Amsterdam. It was the first non-military internet site in Europe in 1988 when the whole of Europe was still connected to the USA with a 64kb link. When designing ABC they were considered completely crazy because it was an interpreted language. Computers were slow at that time. But they knew about Moore's law. Computers would become much faster. At that time computers were very, very expensive. Programmers were basically free. Now it is the other way. Computers are basically free and programmers are very expensive. So, at that time, in the 1950s, programming languages were designed around the needs of the computer, not the programmer. Moore's law is still going strong. Despite many articles claiming its imminent demise. He heard the first one in 1977. Steven showed a graph of his own computers. It fits. On modern laptops, the CPU is hardly doing anything most of the time. So why use programming languages optimized for giving the CPU a rest? There's another cost. The more lines a program has, the …