Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
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 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 … -
A Primer to Django Forms
If you want some interactivity with your users, it all starts with forms. Luckily Django provides some out of the box straightforward solution for us. For this tutorial we are going to do a basic website for surveying a person's age, eye color, name and whether he wants to subscribe or not. If you haven't followed along, you can initiate the tutorial repository if you download it from my Github account. Choose branch exercise3. Further instructions here. So first of all, we have an idea that, we should implement a new feature. For that, we need to create a new “feature branch”. So we can freely experiment, and only merge it when the feature is properly implemented. This new feature will be a form, so let's do this: git checkout -b form git branch You can see that we have, two branches now: * form master As good TDD development practice. Start by writing a test first. New feature deserves it's own test class. Also I know that I will need a new function from main.views. You will see that later. ***main/tests.py*** ... from main.views import home, form … class FormTest(TestCase): def test_form_renders_on_page_properly(self): request = HttpRequest() response = form(request) for … -
RapidCon 2016: RapidPro Developer's Recap
Developer Erin Mullaney was just in Amsterdam for RapidCon, a UNICEF-hosted event for developers using RapidPro, an SMS tool built on Django. The teams that have worked on RapidPro and its predecessor RapidSMS have gotten to know each other virtually over the years. This marks the second time they’ve all come from across the globe to share learnings on RapidPro and to discuss its future. -
How does the Django Cross-site request forgery protection work?
Dan Poirier wrote an article on the Cactus Group blog about common web site security vulnerabilities. In it he talked about the CSRF protection in Django. Although he is right about a CSRF token having to be part of the POST request, this is not the entire story. It is not my intention to claim that mister Poirier does not know how the CSRF protection in Django works. I only want to present a more complete version. First things first, for those of you that have not read the Dan Poiriers article, here’s a short summary of the CSRF related parts. Cross-site request forgery (CSRF or XSRF) is a type of attack where a malicious site is trying to make your browser send requests to another site in an attempt to leverage the permissions of the user—you. (For more information and examples, check the original article or the OWASP page on CSRF.) Besides making sure that GET requests do not change data the article talks about the CSRF protection provided by Django. Specifically it states the following (emphasis mine): Django’s protection is to always include a user-specific, unguessable string as part of such requests, and reject any such request that doesn’t include it. This … -
How does the Django Cross-site request forgery protection work?
Dan Poirier wrote an article on the Caktus Group blog about common web site security vulnerabilities. In it he talked about the CSRF protection in Django. Although he is right about a CSRF token having to be part of the POST request, this is not the entire story. It is not my intention to claim that mister Poirier does not know how the CSRF protection in Django works. I only want to present a more complete version. First things first, for those of you that have not read the Dan Poirier’s article, here’s a short summary of the CSRF related parts. Cross-site request forgery (CSRF or XSRF) is a type of attack where a malicious site is trying to make your browser send requests to another site in an attempt to leverage the permissions of the user—you. (For more information and examples, check the original article or the OWASP page on CSRF.) Besides making sure that GET requests do not change data the article talks about the CSRF protection provided by Django. Specifically it states the following (emphasis mine): Django’s protection is to always include a user-specific, unguessable string as part of such requests, and reject any such request that … -
How to Implement CRUD Using Ajax and Json
Using Ajax to create asynchronous request to manipulate Django models is a very common use case. It can be used to provide an inline edit in a table, or create a new model instance without going back and forth in the website. It also bring some challanges, such as keeping the state of the objects consistent. In case you are not familiar with the term CRUD, it stand for Create Read Update Delete. Those are the basic operations we perform in the application entities. For the most part the Django Admin is all about CRUD. Table of Contents Basic Configuration Working Example Listing Books Create Book Edit Book Delete Book Conclusions Basic Configuration For this tutorial we will be using jQuery to implement the Ajax requests. Feel free to use any other JavaScript framework (or to implement it using bare JavaScript). The concepts should remain the same. Grab a copy of jQuery, either download it or refer to one of the many CDN options. jquery.com/download/ I usually like to have a local copy, because sometimes I have to work offline. Place the jQuery in the bottom of your base template: base.html {% load static %}<!DOCTYPE html> <html lang="en"> <head> <meta … -
Gitのチートシート
GitのGUIはwww.gitkraken.comがおすすめです。 新しいブランチの作成 git branch new_feature ブランチをチェックアウト git checkout new_feature 新しいブランチを作成し、チェックアウトをする git checkout -b new_feature レポジトリのステータスチェック git status すべての変更されたファイルをステージングエリアに追加 git add . 特定のフォルダ、ファイルをステージングエリアに追加 git add test.py ステージングされたファイルをコミット git commit -m "commit message" コミットヒストリーを確認 git log masterブランチに新しいコミットをプッシュする git push origin master Gitのチートシートはw3b.jpで公開された投稿です。 -
Command Line Tricks for Ridiculously Fast Django Development
The command line is one of the most important tool in your arsenal. Knowing it well and be fast with it will seriously boost your performance and effectiveness. One side of that is knowing the commands well, the other side is aliases and custom variables. We will focus on the aliases today with the most important shortcuts. An alias is giving another name to command, possibly a much shorter one. For example you want a faster way to invoke Python interpreter. Instead of “python3” you could just type “p”. The command would go this way: alias p=python3 This setting will cease to exists when you exit the terminal. You can make it permanent if you set them in the .bashrc file in your home directory. Open up ~/.bashrc with your editor. I use nano: nano ~/.bashrc Head to the bottom of the file and copy the following: *** ~/.bashrc *** … #my custom aliases alias v=”source ../virtualenv/bin/activate” alias dea=”deactivate” alias r=”python3 manage.py runserver” alias te=”python3 manage.py test” alias c=”clear” alias mdkir=”mkdir” alias ..=”cd ..” alias ….=”cd ../..” alias …...=”cd ../../..” #my custom variables tut=”~/Tutorial/DjangoTutorial/source” // Replace it where your working directory is If you haven't followed along with the tutorial … -
Command Line Tricks for Ridiculously Fast Django Development
The command line is one of the most important tool in your arsenal. Knowing it well and be fast with it will seriously boost your performance and effectiveness. One side of that is knowing the commands well, the other side is aliases and custom variables. We will focus on the aliases today with the most important shortcuts. An alias is giving another name to command, possibly a much shorter one. For example you want a faster way to invoke Python interpreter. Instead of “python3” you could just type “p”. The command would go this way: alias p=python3 This setting will cease to exists when you exit the terminal. You can make it permanent if you set them in the .bashrc file in your home directory. Open up ~/.bashrc with your editor. I use nano: nano ~/.bashrc Head to the bottom of the file and copy the following: *** ~/.bashrc *** … #my custom aliases alias v=”source ../virtualenv/bin/activate” alias dea=”deactivate” alias r=”python3 manage.py runserver” alias te=”python3 manage.py test” alias c=”clear” alias mdkir=”mkdir” alias ..=”cd ..” alias ….=”cd ../..” alias …...=”cd ../../..” #my custom variables tut=”~/Tutorial/DjangoTutorial/source” // Replace it where your working directory is If you haven't followed along with the tutorial … -
Django Tutorial Setup
This article is an appendix to the other tutorial exercises on the site. Follow these steps to clone my repository from github and make the tutorial setup on your computer. Replace branch “exerciseX” with your current exercise branch. mkdir -p DjangoTutorial/{static,virtualenv,source,database,media} virtualenv --python=python3 DjangoTutorial/virtualenv/ git clone https://github.com/fozodavid/DjangoTutorial.git --branch exerciseX --single-branch DjangoTutorial/source cd DjangoTutorial/source touch MyTutorial/local_settings.py ***MyTutorial/local_settings.py*** import os from MyTutorial.settings import BASE_DIR SECRET_KEY = 'rf@7y-$2a41o+4&z$ki0&=z)(ao=@+$fseu1f3*f=25b6xtnx$' DEBUG = True ALLOWED_HOSTS = [] DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR,'..','database','db.sqlite3'), } } *** end of MyTutorial/local_settings.py *** git branch -m exerciseX master source ../virtualenv/bin/activate pip install django==1.10 deactivate You are ready to start development. -
JSON Web Tokens in django application- part four
When I started this series I have got one comment from my co-worker that instead of authentication JWT can be used to sign one time links. After reading through the documentation I found that can be a great idea so I decided to write a blog post about it. Table of Contents: Use case JSON Web Tokens in urls Other blog posts in this series Use case Nowadays when a user creates an account he or she has to confirm identity. It is done by sending an email with the link to confirm and activate an account. As this link has to expire and be safe this is a good use case for using JSON Web Tokens. Such tokens can be generated for every user and set to expire for example after two hours. How can it be done in Django? Let's jump into the code. JSON Web Tokens in urls First I change the previous code from series and made special django app just for users. But the first user has to register - that's why I made new endpoint in urls.py: from users.views import UserViewSet, CreateUserView, urlpatterns = [ # rest of url patterns url('^api-register/$', CreateUserView.as_view()), ] CreateUserView … -
JSON Web Tokens in django application- part four
When I started this series I have got one comment from my co-worker that instead of authentication JWT can be used to sign one time links. After reading through the documentation I found that can be a great idea so I decided to write a blog post about it. Table … -
JSON Web Token (JWT) Authentication in a Django/AngularJS web app
No matter if you are an experienced developer or if you are starting your first app, there is a task that we all face someday in our life as developers: user’s authentication. Nowadays, there are several kinds of authentication techniques available, and many of them could fit your needs. Nevermind, this post is not about authentication mechanisms, it is about how to implement JSON Web Token Authentication in an application with a Django-based backend, using a REST API to offer resources for an AngularJS frontend app (which fits very well in the Octobot’s technologies stack, and maybe in yours) First of all, why JWT? Well, because it is a compact and self-contained way for securely transmitting information between parties as a JSON object. Compact is good (we all know that), but self-contained? The JWT payload contains all the required information about the user, avoiding the need to query the database more than once. This makes JWT lightweight, scalable and easy to use. Once a user was successfully logged in to your application using a username and password, he/she obtains a JWT which should be sent in every further request to the backend as an Authorization Header, and this token will … -
Common web site security vulnerabilities
I recently decided I wanted to understand better what Cross-Site Scripting and Cross-Site Request Forgery were, and how they compared to that classic vulnerability, SQL Injection.