Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Django is Boring, or Why Tech Startups (Should) Use Django
I recently attended Django Under The Hood in Amsterdam, an annual gathering of Django core team members and developers from around the world. A common theme discussed at the conference this year is that “Django is boring.” While it’s not the first time this has been discussed, it still struck me as odd. Upon further reflection, however, I see Django’s “boringness” as a huge asset to the community and potential adopters of the framework. -
Using Fanout.io in Django
Earlier this year we started using Fanout.io in Air Mozilla to enhance the experience for users awaiting content updates. Here I hope to flesh out its details a bit to inspire others to deploy a similar solution. What It Is First of all, Fanout.io is basically a service that handles your WebSockets. You put in some of Fanout's JavaScript into your site that handles a persistent WebSocket connection between your site and Fanout.io. And to push messages to your user you basically send them to Fanout.io from the server and they "forward" it to the WebSocket. The HTML page looks like this: <html> <body> <h1>Web Page</h1> <!-- replace the FANOUT_REALM_ID with the ID you get in the Fanout.io admin page --> <script src="https://{{ FANOUT_REALM_ID }}.fanoutcdn.com/bayeux/static/faye-browser-1.1.2-fanout1-min.js" ></script> <script src="fanout.js"></script> </body> </html> And the fanout.js script looks like this: window.onload = function() { // replace the FANOUT_REALM_ID with the ID you get in the Fanout.io admin page var client = new Faye.Client('https://{{ FANOUT_REALM_ID }}.fanoutcdn.com/bayeux') client.subscribe('/mycomments', function(data) { console.log('Incoming updated data from the server:', data); }) }; And in server it looks something like this: from django.conf import settings import fanout fanout.realm = settings.FANOUT_REALM_ID fanout.key = settings.FANOUT_REALM_KEY def post_comment(request): """A django view function … -
Transcoding with AWS- part two
As I have static and media files integrated with AWS now it's time to transcode them. In this post, I will write a short example of how to integrate AWS ElasticTranscoder with Django application. Table of Contents: Basic terms Code Other blog posts in this series Basic terms ElasticTranscoder allows you to transcode files from your S3 bucket to various formats. To set this service up first you have to create a pipeline. What pipeline is? Basically, it's a workflow- how your transcoder should work. You can create a different pipeline for long content and different for short one. In my application I created the following pipeline: As I have my pipeline configured next step is to create jobs. Jobs are tasks for a transcoder that say which file I want to transcode, to what format or codec I want to do this: PresetID is user created or already existing configuration that defines the format of transcoder output: is it mp4 or maybe flac? What resolution should video files have? All of this is set up in present. As we know basic terms used in AWS Elastic Transcoder let's jump into the code. Code AWS has very good python API … -
Transcoding with AWS- part two
As I have static and media files integrated with AWS now it's time to transcode them. In this post, I will write a short example of how to integrate AWS ElasticTranscoder with Django application. Table of Contents: Basic terms Code Other blog posts in this series Basic terms ElasticTranscoder allows … -
Django, custom SMTP server for logging
One of my client uses google apps, so I use that account to send out mails with Django, which works just fine. However, google silently drops all error mails to the admins, for whatever reason you can't debug and will never hear anything about anyway. As those mails only go to the dev team it doesn't really matter which mailserver is used, so I decided to write my own email handler. To use it add it to the LOGGING settings like you would the normal AdminEmailHandler and add the necessary settings to your configuration. Raw from django.conf import settings from django.core.mail import get_connection from django.utils.log import AdminEmailHandler class MyAdminEmailHandler(AdminEmailHandler): """ Use a custom handler because gmail sucks... """ def connection(self): return get_connection( host=settings.LOG_EMAIL_HOST, port=settings.LOG_EMAIL_PORT, username=settings.LOG_EMAIL_USER, password=settings.LOG_EMAIL_PASSWORD, use_tls=settings.LOG_EMAIL_USE_TLS, ) -
Django, custom SMTP server for logging
One of my client uses google apps, so I use that account to send out mails with Django, which works just fine. However, google silently drops all error mails to the admins, for whatever reason you can't debug and will never hear anything about anyway. As those mails only go to the dev team it doesn't really matter which mailserver is used, so I decided to write my own email handler. To use it add it to the LOGGING settings like you would the normal AdminEmailHandler and add the necessary settings to your configuration. Raw from django.conf import settings from django.core.mail import get_connection from django.utils.log import AdminEmailHandler class MyAdminEmailHandler(AdminEmailHandler): """ Use a custom handler because gmail sucks... """ def connection(self): return get_connection( host=settings.LOG_EMAIL_HOST, port=settings.LOG_EMAIL_PORT, username=settings.LOG_EMAIL_USER, password=settings.LOG_EMAIL_PASSWORD, use_tls=settings.LOG_EMAIL_USE_TLS, ) -
A Quick Look at the Kubernetes Python Client
For those of you that don't know there is a new Python API client in the kubernetes-incubator project: client-python. There has been some high quality Python clients like pykube, but client-python can serve as the official Python client. ## The Structure of the Client client-python is a client that is mostly generated based on the swagger spec (UI). While pykube has the benefit of being totally idiomatic, client-python can support practically all of the endpoints and react quickly to changes in the API. client-python supports Python 3 and is currently tested against Python 3.4. ## Usin[...] -
How to Create Group By Queries With Django ORM
This tutorial is about how to implement SQL-like group by queries using the Django ORM. It’s a fairly common operation, specially for those who are familiar with SQL. The Django ORM is actually an abstraction layer, that let us play with the database as it was object-oriented but in the end it’s just a relational database and all the operations are translated into SQL statements. Most of the work can be done retrieving the raw data from the database, and playing with it in the Python side, grouping the data in dictionaries, iterating through it, making sums, averages and what not. But the database is a very powerful tool and do much more than simply storing the data, and often you can do the work much faster directly in the database. Generally speaking, when you start doing group by queries, you are no longer interested in each model instances (or in a table row) details, but you want extract new information from your dataset, based on some common aspects shared between the model instances. Let’s have a look in an example: class Country(models.Model): name = models.CharField(max_length=30) class City(models.Model): name = models.CharField(max_length=30) country = models.ForeignKey(Country) population = models.PositiveIntegerField() And the raw … -
Transcoding with AWS- part one
Nowadays moving everything to the cloud becomes more and more popular. A lot of software companies move their technology stack to such infrastructure. One of the biggest players in this field is Amazon Web Services - AWS. That's why I decided decided to adapt existing code from my previous project and move transcoding to write blog posts about that. In this series I process to cloud. Table of Contents: Overview of series Moving static and media files to AWS Overview of series I decided to adapt code from my previous blog series about celery and rabbit-mq. I did that because code from this django application actually transcodes mp3 files to other formats. This series will be divided into these parts: Moving static and media files to AWS Transcoding files inside AWS transcoder Notifying user that transcode is complete User downloads transcoded file Moving static and media files to AWS AWS transcoder operates only on files that are inside S3 bucket so first I need to change how these files are served in django. Let's say that I already had my account on AWS. Next step is to generate specific account using IAM. While creating a user I want to give … -
Transcoding with AWS- part one
Nowadays moving everything to the cloud becomes more and more popular. A lot of software companies move their technology stack to such infrastructure. One of the biggest players in this field is Amazon Web Services - AWS. That's why I decided decided to adapt existing code from my previous project and … -
Birthday retrospective
So, recently Evennia celebrated its ten-year anniversary. That is, it was on Nov 20, 2006, Greg Taylor made the first repo commit to what would eventually become the Evennia of today. Greg has said that Evennia started out as a "weird experiment" of building a MUD/MUX using Django. The strange name he got from a cheesy NPC in the Guild Wars MMORPG and Greg's first post to the mailing list also echoes the experimental intention of the codebase. The merger with Twisted came pretty early too, replacing the early asyncore hack he used and immediately seeing a massive speedup. Evennia got attention from the MUD community - clearly a Python-based MUD system sounded attractive.When I first joined the project I had been looking at doing something MUD-like in Python for a good while. I had looked over the various existing Python code bases at the time and found them all to be either abandoned or very limited. I had a few week's stunt working with pymoo before asking myself why I was going through the trouble of parsing a custom script language ... in Python ... Why not use Python throughout? This is when I came upon Evennia. I started … -
Optimizing the construction of Django QuerySets
Django’s ORM is normally fast enough as-is, but if you’ve ever profiled a high traffic view with a fairly complicated query, you might have found that constructing QuerySet can take a noticeable portion of your request time. For example, I once found a query on the front page of the site I was working on that took 1ms to construct and 1ms for the database to answer. With a performance budget of 100ms, that was 1% gone on computing the exactly same SQL. Thankfully we don’t need to instantly drop down to raw SQL to optimize such cases, as Django’s QuerySet API naturally lends itself to caching the intermediate objects. Since each operation on a QuerySet returns a new object with the change applied, they’re always lazy as to executing the SQL, and operations can (normally) be chained in any order, you can build the non-specific part of your QuerySet up as a cached object and then apply final, specific filtering required at request time. Just a note before we dive in: this should be one of the least reached for tools in your optimization toolbox - normally it’s enough to fix the basics such as avoiding N+1 queries with … -
How to reset migrations in Django 1.7 - 1.8 - 1.9 and above
Migrations help you propagate models changes to your database schema,they are particularly helpful in the situation when you need to change your database structure and you don't want or you can't drop a database table and recreate it or when you have a production database with tables which has millions of rows .Any developer has experienced situations where he has to change the structure of an existing table such as adding,deleting or renaming a field.In some cases droping the table and recreate it solve the problem and release the developer from the headache related to and resulted by the process but just imagine a scenario where your application is already in production with millions of database rows ,droping your old tables is not a choice.You can't even dear to think about it so migrations are here to present you a more acceptable and professional solution. Simply migrations lets you change your database schema while keeping your data. How to get started with migrations ? Getting started with migrations is easy especially with the latest versions of Django,starting with Django 1.7 .In fact from Django 1.7 migrations become obligatory since they are integrated within your django workflow. To work with migrations … -
Django Under the Hood 2016 Recap
Caktus was a proud sponsor of Django Under the Hood (DUTH) 2016 in Amsterdam this year. Organized by Django core developers and community members, DUTH is a highly technical conference that delves deep into Django. -
Introduction to Django Models
In this part of the tutorial, I will show you some basic model definition by creating a simple blog. As discussed earlier, database tables are translated from the models.py files. If you haven't followed along with earlier tutorials, you can do the setup here. Choose branch exercise4. As usual we start by creating a new branch: git checkout -b blog Activate virtualenv, if you haven't already: source ../virtualenv/bin/activate A is an application by itself, so we create one inside Django with the following command. python3 manage.py startapp blog You should see these directories and files in your source dir: blog/ main/ manage.py MyTutorial/ requirements.txt In order for django to discover the new application, you need to include it in the installed apps in settings.py ***MyTutorial/settings.py*** ... INSTALLED_APPS = [ 'main', 'blog', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] … As usual we write some tests first. If you have a look at the other test file main/tests.py you can see, that we have some very similar goal here. It might be tempting to import those tests and tweak it a little, but that wouldn't be right. As for Unit Tests, it needs to be completely independent from each other. So … -
How to break Python
Don’t worry, this isn’t another piece about Python 3. I’m fully in favor of Python 3, and on record as to why. And if you’re still not convinced, I suggest this thoroughly comprehensive article on the topic, which goes over not just the bits people get angry about but also the frankly massive amount of cool stuff that only works in Python 3, and that you’re missing out on if you still only use Python 2 ... Read full entry -
How to break Python
Don’t worry, this isn’t another piece about Python 3. I’m fully in favor of Python 3, and on record as to why. And if you’re still not convinced, I suggest this thoroughly comprehensive article on the topic, which goes over not just the bits people get angry about but also the frankly massive amount of cool stuff that only works in Python 3, and that you’re missing out on if you still only use Python 2 ... Read full entry -
How to Filter QuerySets Dynamically
Filtering QuerySets dynamically is a fairly common use case. Sure thing there is a pluggable app to make your life easier. This tutorial is about how to use the django-filter app to add a hassle-free filtering to your views. To illustrate this tutorial I will implement a view to search for users. As usual the code used in this tutorial is available on GitHub. You can find the link in the end of this post. Installation Easiest way is to install it with pip: pip install django-filter That’s it. It’s ready to be used. Make sure you update your requirements.txt. The default language of the app is English. It already come with some localization and language support. Currently the supported languages are: de, es_AR, fr, pl, ru, zh_CN. Unless you want to use any of those languages in your project, you don’t need to add django_filter to the INSTALLED_APPS. Usage Create a file named filters.py inside your app folder: filters.py from django.contrib.auth.models import User import django_filters class UserFilter(django_filters.FilterSet): class Meta: model = User fields = ['username', 'first_name', 'last_name', ] The view is as simple as: views.py from django.contrib.auth.models import User from django.shortcuts import render from .filters import UserFilter def search(request): … -
Getting started with pytest
Pytest is my preferred Python testing library. It makes simple tests incredibly easy to write, and is full of advanced features (and tons of plugins) that help with more advanced testing scenarios. To demonstrate the basics, I’m going to walk through how I’d solve the first couple cryptopals challenges in a test-driven style, using py.test. Spoiler alert: I’m going to spoil the first challenge, and maybe a bit of the second, below. -
Django Channels: Using Custom Channels
In my earlier blog post - Introduction to Django Channels, I mentioned that we can create our own channels for various purposes. In this blog post, we would discuss where custom channels can be useful, what could be the challenges and of course we would see some code examples. But before we begin, please make sure you are familiar with the concepts of Django Channels. I would recommend going through the above mentioned post and the official docs to familiarize yourself with the basics. Our Use Case Channels is just a queue which has consumers (workers) listenning to it. With that concept in mind, we might be able to think of many innovative use cases a queue could have. But in our example, we will keep the idea simple. We are going to use Channels as a means of background task processing. We will create our own channels for different tasks. There will be consumers waiting for messages on these channels. When we want to do something in the background, we would pass it on the appropriate channels & the workers will take care of the tasks. For example, we want to create a thumbnail of an user uploaded photo? … -
How to Add User Profile To Django Admin
There are several ways to extend the the default Django User model. Perhaps one of the most common way (and also less intrusive) is to extend the User model using a one-to-one link. This strategy is also known as User Profile. One of the challenges of this particular strategy, if you are using Django Admin, is how to display the profile data in the User edit page. And that’s what this tutorial is about. Background I’ve published a while ago an article about How to Extend Django User Model, describing in great detail all the different strategies. If you are still not familiar with it, I strongly suggest that you have a look in this article. This tutorial is about the User Profile strategy. So, consider we have an app named core with the following model definition: models.py from django.contrib.auth.models import User from django.db import models from django.db.models.signals import post_save from django.dispatch import receiver class Profile(models.Model): STUDENT = 1 TEACHER = 2 SUPERVISOR = 3 ROLE_CHOICES = ( (STUDENT, 'Student'), (TEACHER, 'Teacher'), (SUPERVISOR, 'Supervisor'), ) user = models.OneToOneField(User, on_delete=models.CASCADE) location = models.CharField(max_length=30, blank=True) birthdate = models.DateField(null=True, blank=True) role = models.PositiveSmallIntegerField(choices=ROLE_CHOICES, null=True, blank=True) def __str__(self): # __unicode__ for Python 2 return … -
Django Multiple Files Upload Using Ajax
In this tutorial I will guide you through the steps to implement an AJAX multiple file upload with Django using jQuery. For this tutorial we will be using a specific plug-in called jQuery File Upload, which takes care of the server communication using AJAX and also the compatibility with different browsers. The plug-in is great, but it have so many features that sometimes it can become challenging for some to get started. You will notice that some of the examples are a little bit redundant, repeating code and so on. That’s on purpose, so to avoid code abstraction and the examples become more clear. In the end of this post you will also find the link to download all the code used in this tutorial. Basic Configuration Before you move forward, if you are not familiar at all with file upload with Django, it is a good a idea to check this post I published while ago: How to Upload Files With Django. It will give you an overview of the basics and some caveats. To work with file upload you will need to set the MEDIA_URL and MEDIA_ROOT. settings.py MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media') And to test … -
Django Under the Hood 2016 Highlights
Videos from Django Under the Hood 2016 are up - check ‘em out! As usual, the conference was amazing and the content was fantastic. I really enjoyed all the talks, and they’re all worth your time to talk. Three in particular stood out to me as exceptional highlights: Ana’s talk on Testing in Django is the single best talk on effective testing of Django apps I’ve ever seen. I really like her technique of explaining Django’s testing APIs by looking at how they changed over time: it does a great job of explaining what problems particular APIs solve, and why you’d use them. -
Django Grils- Kraków #3
As I said many times on this blog I really like teaching others so I can improve myself. That's why when I heard about Django Girls Kraków I didn't hesitate and I joined this event as a coach. This is short recap from Django Girls Kraków #3. Table of Contents: Installation party Workshop day Conclusion Installation party The main event was held on Saturday but the day before there was a small installation party when for two hours girls were installing necessary tools for workshops such as python, django virtualenv and git. When it comes to my team there were 3 girls on it: Joanna, Olga and Magda. Before the Django Girls organizators came up with a wonderful idea that to get to know everyone in the team a little bit better, every person has to write a few sentences about themselves. Thanks to that there were already conversation starters. The installation went well without any major problems (considered that girls used Windows). After the installation party there was a pleasant surprise - dinner for coaches to thank for their work. Super cool! Workshop day Workshops started early - at 9 am. Girls started working on django girls tutorial. I … -
Django Grils- Kraków #3
As I said many times on this blog I really like teaching others so I can improve myself. That's why when I heard about Django Girls Kraków I didn't hesitate and I joined this event as a coach. This is short recap from Django Girls Kraków #3. Table of Contents …