Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Code, Code, Code
I'm often asked by new programmers how they can forge a path into using their skills professionally. Or how they can get better at writing software. How to Improve Your Coding Skills This was my path. It may not be your path. I coded. A lot. From silly little scripts to automating tasks to attempting full-blown projects. At work or for fun. I failed a lot, but learned along the way. I didn't jump from language to language. Instead I stayed in a few places for years and focused my learning on those tools. My 19+ year career can be summed up as FoxPro then Java then Python. In the middle of things I picked up JavaScript. Sure, I've dallied with a few things (Lisp, Haskell, Lua, Perl, ColdFusion, Go), but by staying focused on a small set of tools I'm better than mediocre. I coded lots. Yes, this is a repeat of #1. Once I got the basics of a language, I looked up best practices for each of them. Then I religiously adhered to them, even becoming dogmatic about it. In general this means my code is more easily read. More easily debugged. And most importantly, more easily … -
Get count, average, min, max values from model field using Django Aggregate
Django queries help to create, retrieve, update and delete objects. But sometimes we need to get summered values from the objects. Then a Simple solution is to use Django aggregate feature Here are simple examples of how to use aggregation. app/models.py class company(models.Model): name=models.CharField(max_length=20) est=models.IntegerField(max_length=4) class feature(models.Model): name=models.CharField(max_length=20) class device(models.Model): name=models.CharField(max_length=20) Code=models.IntegerField() Company=models.ForeignKey(company) features=models.ManyToManyField(feature) To perform Min Max and Avg on specific column >>> from django.db.models import Avg, Max, Min, Sum >>> device.objects.all().aggregate(Avg('price')) {'price__avg': 12234.0} >>> device.objects.all().aggregate(Max('price')) {'price__max':587961 } >>> device.objects.all().aggregate(Min('price')) {'price__min': 01245} To know the children count of a foreign key field. >>> Comp_Devices=company.objects.annotate(no_of_devices=Count('device')) >>> Comp_Devices[0].no_of_devices 36 >>> Comp_Devices[0].name u'Micro' >>> Comp_Devices[1].no_of_devices 1 the same procedure can be followed for ManyToMany field Columns >>> Dev_features_count = device.objects.annotate(num=Count('features')) >>> Dev_features_count[0].num 3 >>> Dev_features_count[1].num 2 >>> You can perform some complex queries like To know the devices which have more than two features >>> Dev_features= device.objects.annotate(num=Count('features')).filter(num__gt=2) >>> Dev_features[0].num 3 >>> -
Querying with Django Q objects
Django Q objects: Q object encapsulates a SQL expression in a Python object that can be used in database-related operations. Using Q objects we can make complex queries with less and simple code. For example, this Q object filters whether the question starts wiht 'what': from django.db.models import Q Q(question__startswith='What') Q objects are helpfull for complex queries because they can be combined using logical operators and(&), or(|), negation(~) For example, this statement returns if the question starts with 'who' or with 'what'. Q(question__startswith='Who') | Q(question__startswith='What') Note: If the operator is not included then by default 'AND' operator is used The following code is source of Q class: class Q(tree.Node): AND = 'AND' OR = 'OR' default = AND def __init__(self, *args, **kwargs): super(Q, self).__init__(children=list(args) + list(six.iteritems(kwargs))) def _combine(self, other, conn): if not isinstance(other, Q): raise TypeError(other) obj = type(self)() obj.connector = conn obj.add(self, conn) obj.add(other, conn) return obj def __or__(self, other): … -
How to add CORS headers to a Django view for use with a Cordova app
Suppose you want to access some JSON data from a mobile app using Cordova. You have to bypass CORS restrictions in the web view, and to do that you have to provide some HTTP headers in your Django views. You can do this pretty easily by using this mixin in your Class Based Views: from django.http import HttpResponse class AllowCORSMixin(object): def add_access_control_headers(self, response): response["Access-Control-Allow-Origin"] = "*" response["Access-Control-Allow-Methods"] = "GET, OPTIONS" response["Access-Control-Max-Age"] = "1000" response["Access-Control-Allow-Headers"] = "X-Requested-With, Content-Type" def options(self, request, *args, **kwargs): response = HttpResponse() self.add_access_control_headers(response) return response Use it like in the example below. Here used together with Django Braces app: from django.views.generic import View from braces.views import AjaxResponseMixin, JSONResponseMixin class JsonView(JSONResponseMixin, AjaxResponseMixin, AllowCORSMixin, View): def get_ajax(self, request, *args, **kwargs): # ... get some data ... response = self.render_json_response({ 'data': data }) self.add_access_control_headers(response) return response -
Pretty Formatting JSON in the Django Admin
Recently I was writing code to interact with a third-party API. The API changes frequently, especially the data contained in responses. However, that data has to be saved and periodically needs to be audited. I wanted a data model flexible enough to handle these periodic changes without a lot of anguish, yet queryable. Since the API serves out queryable JSON, this made it a no-brainer for using django.contrib.postgres's JSONField. After a little bit of work, I had data samples to play with. Quickly my admin filled with chunks of JSON that looked something like this: {"field_12": 8, "field_16": 4, "field_6": 14, "field_7": 13, "field_18": 2, "field_2": 18, "field_4": 16, "field_15": 5, "field_9": 11, "field_3": 17, "field_8": 12, "field_11": 9, "field_17": 3, "field_10": 10, "field_0": 20, "field_1": 19, "field_13": 7, "field_5": 15, "field_14": 6} Kind of illegible, right? And that's a simple, flat example with just 20 keys. Imagine if this were a nested dictionary with 100 or 200 fields. For reference, that's the kind of data that I had that makes this kind of display nigh useless. So I cooked up this quick fix: import json from pygments import highlight from pygments.lexers import JsonLexer from pygments.formatters import HtmlFormatter from django.contrib … -
Running Django with PyPy to boost performance
PyPy: PyPy is an alternative python interpreter which focuses on speed and memory. PyPy uses JIT compiler. PyPy is a replacement for CPython. It is built using the RPython language that was co-developed with it. The main reason to use it instead of CPython is speed. PyPy Installation: Ubuntu 12.04 - 14.04: Download https://bitbucket.org/pypy/pypy/downloads/pypy3-2.4.0-linux64.tar.bz2 Now you can uncompress them either somewhere in your home directory or, say, in /opt, and if you want, put a symlink from somewhere like /usr/local/bin/pypy to /path/to/pypy-5.1.1/bin/pypy. Do not move or copy the executable pypy outside the tree – put a symlink to it, otherwise it will not find its libraries. ArchLinux: pacman -Sy pypy # for python2 pacman -Sy pypy3 # for python3 Let's Create and run pypy_django app: 1. Create pypy virtualenv: Run the following command to create pypy based virtualenv: virtualenv -p /usr/local/bin/pypy env # if you used different path for pypy installation change the path as required 2. Install django: First activate the env and then run the follwing command to install django pip install django 3. Create a django project … -
Pretty Formatting JSON in the Django Admin
Recently I was writing code to interact with a third-party API. The API changes frequently, especially the data contained in responses. However, that data has to be saved and periodically needs to be audited. I wanted a data model flexible enough to handle these periodic changes without a lot of anguish, yet queryable. Since the API serves out queryable JSON, this made it a no-brainer for using django.contrib.postgres's JSONField. After a little bit of work, I had data samples to play with. Quickly my admin filled with chunks of JSON that looked something like this: {"field_12": 8, "field_16": 4, "field_6": 14, "field_7": 13, "field_18": 2, "field_2": 18, "field_4": 16, "field_15": 5, "field_9": 11, "field_3": 17, "field_8": 12, "field_11": 9, "field_17": 3, "field_10": 10, "field_0": 20, "field_1": 19, "field_13": 7, "field_5": 15, "field_14": 6} Kind of illegible, right? And that's a simple, flat example with just 20 keys. Imagine if this were a nested dictionary with 100 or 200 fields. For reference, that's the kind of data that I had that makes this kind of display nigh useless. So I cooked up this quick fix: import json from pygments import highlight from pygments.lexers import JsonLexer from pygments.formatters import HtmlFormatter from django.contrib … -
Pretty Formatting JSON in the Django Admin
Recently I was writing code to interact with a third-party API. The API changes frequently, especially the data contained in responses. However, that data has to be saved and periodically needs to be audited. I wanted a data model flexible enough to handle these periodic changes without a lot of anguish, yet queryable. Since the API serves out queryable JSON, this made it a no-brainer for using django.contrib.postgres's JSONField. After a little bit of work, I had data samples to play with. Quickly my admin filled with chunks of JSON that looked something like this: {"field_12": 8, "field_16": 4, "field_6": 14, "field_7": 13, "field_18": 2, "field_2": 18, "field_4": 16, "field_15": 5, "field_9": 11, "field_3": 17, "field_8": 12, "field_11": 9, "field_17": 3, "field_10": 10, "field_0": 20, "field_1": 19, "field_13": 7, "field_5": 15, "field_14": 6} Kind of illegible, right? And that's a simple, flat example with just 20 keys. Imagine if this were a nested dictionary with 100 or 200 fields. For reference, that's the kind of data that I had that makes this kind of display nigh useless. So I cooked up this quick fix: import json from pygments import highlight from pygments.lexers import JsonLexer from pygments.formatters import HtmlFormatter from django.contrib … -
Where to Find Cakti at PyCon 2016
As Django developers, we always look forward to PyCon. This year, working with the Python Software Foundation on the design for PyCon 2016’s site kindled our enthusiasm early. Our team is so excited for all the fun to begin. With an array of fantastic events, speakers, and workshops, we thought we would highlight all the events we’ll be participating in. Come find us! -
So you want a new admin?
Django’s admin site is about 12 years old. It started circa early 2004 as an internal CMS at the Lawrence Journal-World, was released as part of Django in 2005, and has been chugging away ever since. There’s been some substantial re-writes along the way – magic-removal, new forms, the flat theme – but for the most part the admin’s stayed pretty much the same for that entire time. The interface, functionality, and CRUD-oriented workflow haven’t really changed since those early years at the Journal-World. -
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': { … -
Working with Django Plugins
Django-plugins is a package that helps you to build apps more reusable. It is currently tested with Python 2.7, 3.2, 3.3, and 3.4 along with Django versions 1.7 and 1.8. It might well work with other versions. By using 'Django-plugins', you can define, access plugins and plugin points. Installation: $ pip install django-plugins * Plugins are stored as python objects and synchronized to database called plugin models(PluginPoint, Plugin). * Both Plugin and plugin models has a name and title attributes. And each plugin point/plugin can be marked as enabled/disabled/removed using 'status' field in respective models from Django admin panel. * Plugins are the classes which are hardcoded, which means they cannot be modified directly by the users. But they can change the database objects of those plugins. In this way, you can provide the flexibility to the user to change plugins. NOTE: You should always use plugin attributes (name, title) from model objects but not from plugins. * All defined plugins and plugin points are synchronized to database tables using Django management command 'sync plugins' or 'sync DB'. 'sync plugins' command detects the removed plugin points, plugins and marks them as 'REMOVED' in the database. Features of 'Django-plugins': Synchronization with … -
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
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. … -
Ports and Adapters in python - part one
First part of series about Django application made using Ports and Adapters design pattern. -
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 │ … -
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