Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
How to Use RESTful APIs with Django
First, let’s get those terms out of our way. The REST acronym stands for Representational State Transfer, which is an architectural design. Usually when we use the term RESTful, we are referring to an application that implements the REST architectural design. API stands for Application Programming Interface, which is a software application that we interact programmatically, instead of using a graphical interface. In other words, we interact with it at a lower level at the source code, writing functions and routines. In the context of Web development, usually when we are talking about a RESTful API we are referring to Web Services (or Web APIs). It’s a common way to expose parts of your application to third-parties (external applications and Websites). It can be data-oriented, in a sense that your Web service (the RESTful API), simply make available the information you store in your databases using a common format, such as XML or JSON. This way, an external application can interact with your application and your data, without having to connect directly into your database. This way, it doesn’t matter if your database is MySQL or PostgreSQL, or if your application was written in Java or Python. But RESTful APIs … -
Implemementing Manual Schema with Django REST Framework
This is what will hopefully be the first in a series of reference articles for using Core API libraries with Django REST Framework (DRF). This is an extraction from an existing production view running Django 1.11/2.0 on Python 3.6. The original code did something else, but for contract reasons I'm demonstrating this code with sending email. Please note that this article is very terse, with almost no description, no tests, and no URL routing. Just enough code so that if you have a decent understand of DRF, you can make custom views work with Core API. First, the serializer: # serializers.py from django.core.mail import send_mail from markdown import markdown from rest_framework import serializers class EmailSerializer(serializers.Serializer): to_addresses = serializers.ListField( child=serializers.EmailField(), required=True ) from_email = serializers.EmailField(required=True) subject = serializers.CharField(required=True) message = serializers.CharField(required=True) htmlize = serializer.BooleanField(required=False, default=False) def create(self, validated_data): if validated_data['htmlize']: validated_data['html_message'] = markdown(validated_data['message']) send_mail(**validated_data) Now the view: # views.py import coreapi import coreschema from rest_framework import schemas from rest_framework.views import APIView from .serializers import EmailSerializer class EmailCreateAPIView(APIView): """ Assumes you have set permissions and authentication in `settings.py`""" serializers = EmailSerializer schema = schemas.ManualSchema(fields=[ coreapi.Field( "to_addresses", required=True, location="form", schema=coreschema.Array( description="List of email addresses" ) ), coreapi.Field( "from_email", required=True, location="form", schema=coreschema.String() ), coreapi.Field( … -
Implemementing Manual Schema with Django REST Framework
This is what will hopefully be the first in a series of reference articles for using Core API libraries with Django REST Framework (DRF). This is an extraction from an existing production view running Django 1.11/2.0 on Python 3.6. The original code did something else, but for contract reasons I'm demonstrating this code with sending email. Please note that this article is very terse, with almost no description, no tests, and no URL routing. Just enough code so that if you have a decent understand of DRF, you can make custom views work with Core API. First, the serializer: # serializers.py from django.core.mail import send_mail from markdown import markdown from rest_framework import serializers class EmailSerializer(serializers.Serializer): to_addresses = serializers.ListField( child=serializers.EmailField(), required=True ) from_email = serializers.EmailField(required=True) subject = serializers.CharField(required=True) message = serializers.CharField(required=True) htmlize = serializer.BooleanField(required=False, default=False) def create(self, validated_data): if validated_data['htmlize']: validated_data['html_message'] = markdown(validated_data['message']) send_mail(**validated_data) Now the view: # views.py import coreapi import coreschema from rest_framework import schemas from rest_framework.views import APIView from .serializers import EmailSerializer class EmailCreateAPIView(APIView): """ Assumes you have set permissions and authentication in `settings.py`""" serializers = EmailSerializer schema = schemas.ManualSchema(fields=[ coreapi.Field( "to_addresses", required=True, location="form", schema=coreschema.Array( description="List of email addresses" ) ), coreapi.Field( "from_email", required=True, location="form", schema=coreschema.String() ), coreapi.Field( … -
File Upload with Angular
<div class='alert alert-warnin... -
DRF with Marshmallow Serializers for Fun and Profit
The Problem Django Rest Framework (DRF) is a fantastic tool for API development in Django. However, it does have some downsides. Serializers are an especially heavy part of the framework. Rightfully so, they do quite a bit of magic to make API development lightning quick by automatically pulling fields from your Django Models and applying all the database validation. Another downside is that you are forced to declare what fields you want to serializer in the class definition. This typically means you include everything if you want to reuse the same endpoint, or you have to create separate serializers for different views. # everything including the kitchen sink class BlogSerializer(serializers.ModelSerializer): class Meta: model = Blog fields = '__all__' # oops I don't want author private notes and editor notes # passed to regular consumers of the API endpoint class PublicBlogSerializer(serializers.ModelSerializer): class Meta: model = Blog exclude = ('private_notes', 'editor_notes', ) # ...but now I need a serializer for my editor that includes the editor_notes class EditorBlogSerializer(serializers.ModelSerializer): class Meta: model = Blog exclude = ('private_notes', ) Wow! What a mess! Three serializers for essentially the same exact thing just +/- a few fields. The Solution Introducing Marshmallow! Marshmallow serializers are agnostic … -
Creating an JWT Token Interceptor for HTTP Requests in Angular
Below is a snippet that's usef... -
Using setup.py in Your (Django) Project
A client recently asked me why all the Django projects we create have a setup.py in the root of the project. Lots of projects get by just fine without one, so why do I use it? The explanation turned out a bit long-winded, so I thought I'd turn it into a blog post here. What is setup.py? According to Python's official docs: The setup script is the centre of all activity in building, distributing, and installing modules using the Distutils. The main purpose of the setup script is to describe your module distribution to the Distutils, so that the various commands that operate on your modules do the right thing. Distutils is part of the Python standard library and is extended by tools you're probably more familiar with like setuptools, pip, and pipenv. Every package you download off of the Python Package Index (PyPI) has a setup.py. It is used by pipenv/pip/setuptools/distutils to figure out where the Python module is in your project, what dependencies it needs, what scripts it needs to install, and more. Why? So why should a Django project use setup.py? Django is Just Python While very early versions of Django eschewed many Python best-practices, this was … -
Using setup.py in Your (Django) Project
A client recently asked me why all the Django projects we create have a setup.py in the root of the project. Lots of projects get by just fine without one, so why do I use it? The explanation turned out a bit long-winded, so I thought I'd turn it into a blog post here. What is setup.py? According to Python's official docs: The setup script is the centre of all activity in building, distributing, and installing modules using the Distutils. The main purpose of the setup script is to describe your module distribution to the Distutils, so that the various commands that operate on your modules do the right thing. Distutils is part of the Python standard library and is extended by tools you're probably more familiar with like setuptools, pip, and pipenv. Every package you download off of the Python Package Index (PyPI) has a setup.py. It is used by pipenv/pip/setuptools/distutils to figure out where the Python module is in your project, what dependencies it needs, what scripts it needs to install, and more. Why? So why should a Django project use setup.py? Django is Just Python While very early versions of Django eschewed many Python best-practices, this was … -
How to Implement Dependent/Chained Dropdown List with Django
Dependent or chained dropdown list is a special field that relies on a previously selected field so to display a list of filtered options. A common use case is on the selection of state/province and cities, where you first pick the state, and then based on the state, the application displays a list of cities located in the state. Example Scenario Take the application below as an example: models.py from django.db import models class Country(models.Model): name = models.CharField(max_length=30) def __str__(self): return self.name class City(models.Model): country = models.ForeignKey(Country, on_delete=models.CASCADE) name = models.CharField(max_length=30) def __str__(self): return self.name class Person(models.Model): name = models.CharField(max_length=100) birthdate = models.DateField(null=True, blank=True) country = models.ForeignKey(Country, on_delete=models.SET_NULL, null=True) city = models.ForeignKey(City, on_delete=models.SET_NULL, null=True) def __str__(self): return self.name In the application we are going to create a simple form processing to create and update person objects. The dependent dropdown list will be used on the country and city fields of the person model. urls.py from django.urls import include, path from . import views urlpatterns = [ path('', views.PersonListView.as_view(), name='person_changelist'), path('add/', views.PersonCreateView.as_view(), name='person_add'), path('<int:pk>/', views.PersonUpdateView.as_view(), name='person_change'), ] Finally, the three basic views: views.py from django.views.generic import ListView, CreateView, UpdateView from django.urls import reverse_lazy from .models import Person class PersonListView(ListView): model = … -
Kicking into gear from a distance
The last few weeks I have reworked the way Evennia's startup procedure works. This is now finished in the develop branch so I thought I'd mention a little what's going on.Evennia, being a server for creating and running text-games (MU*s), consists of two main processes:The Portal - this is what players connect to with their clients. The Server - this is the actual game, with the database etc. This can be shutdown and started again without anyone connected to the Portal getting kicked from the game. This allows for hot-adding new Python code into the running Server without any downtime. Since Evennia should be easy to set up and also run easily on Windows as well as on Linux/Mac, we have foregone using the linux process management services but instead offered our own solution. This is how the reload mechanism currently looks in master branch:Here I've excluded connections irrelevant to reloading, such as the Twisted AMP connection between Portal and Server. Dashed lines suggest a more "temporary" connection than a solid line.The Launcher is the evennia program one uses to interact with the Server in the terminal/console. You give it commands like evennia start/stop/reload.When starting, the Launcher spawns a new program, … -
Install Tensorflow GPU on Windows using CUDA and cuDNN
So I picked myself up a [GeFor... -
When to Use MongoDB with Django
Short Answer You don't. Long Answer First off, let's get one thing out of the way. This isn't a bash on MongoDB. MongoDB works great with lots of things (Flask, Tornado, Node, etc), but it's a mismatch with Django. In other words, this article is about using the right tool for the right job. Second, I'm not speaking from ignorance. In fact, I have quite a bit of experience combining MongoDB and Django. You can see some of my early work with combining these tools in the defunct django-mongonaut. Okay then, let's get into the background of this post: On various Django help forums, you'll hear requests from new-ish Django developers on how to use MongoDB with Django. Most of the time they want to replace the Django ORM with calls to MongoDB. Here are the reasons I've heard so far. The 90% Reason: JSON storage Most of the time people want to replace SQL with MongoDB in Django, the reason is they want to store JSON data and search it. In which case, they should use Django's built-in implementation of PostgreSQL's JSON field. It's not just a string field, it's fully searchable. Implementation example below: from django.contrib.postgres.fields import JSONField … -
When to Use MongoDB with Django
Short Answer You don't. Long Answer First off, let's get one thing out of the way. This isn't a bash on MongoDB. MongoDB works great with lots of things (Flask, Tornado, Node, etc), but it's a mismatch with Django. In other words, this article is about using the right tool for the right job. Second, I'm not speaking from ignorance. In fact, I have quite a bit of experience combining MongoDB and Django. You can see some of my early work with combining these tools in the defunct django-mongonaut. Okay then, let's get into the background of this post: On various Django help forums, you'll hear requests from new-ish Django developers on how to use MongoDB with Django. Most of the time they want to replace the Django ORM with calls to MongoDB. Here are the reasons I've been quoted. The 90% Reason: JSON storage Most of the time people want to replace SQL with MongoDB in Django, the reason is they want to store JSON data and search it. In which case, they should use Django's built-in implementation of PostgreSQL's JSON field. It's not just a string field, it's fully searchable. Example below: from django.contrib.postgres.fields import JSONField from django.db … -
Automatic Created and Updated DateTime Fields for Django Models
I'm leaving this to my future self, as I keep forgetting it over and over again. If you want to add "create" and "update" time logging to your model, it's as simple as: from django.db import models class Task(models.Model): created = models.DateTimeField(auto_now_add=True) updated ... Read now -
Django 2.0 support is here!
Long time no blog.. However, this one brings some good news! Most of our packages have been updated to support Django 2.0. They are easily to recognize, because all upgraded packages have been given a “2.0” version number. This choice simplifies the version management across all packages, and marks a clear support baseline. The 2.0 packages support Django 1.10, 1.11 and 2.0. If you still run on Django 1.8, you can use the 1.x series. There have been a few backports to ease the transition for old projects. Available are: .hll { background-color: #ffffcc } .code149-c { color: #408080; font-style: italic } /* Comment */ .code149-err { border: 1px solid #FF0000 } /* Error */ .code149-k { color: #008000; font-weight: bold } /* Keyword */ .code149-o { color: #666666 } /* Operator */ .code149-ch { color: #408080; font-style: italic } /* Comment.Hashbang */ .code149-cm { color: #408080; font-style: italic } /* Comment.Multiline */ .code149-cp { color: #BC7A00 } /* Comment.Preproc */ .code149-cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */ .code149-c1 { color: #408080; font-style: italic } /* Comment.Single */ .code149-cs { color: #408080; font-style: italic } /* Comment.Special */ .code149-gd { color: #A00000 } /* Generic.Deleted */ .code149-ge … -
Handling CORS in Django REST Framework
If you are building applications with Django and modern front-end/JavaScript technologies such as Angular, React or Vue, chances are that you are using two development servers for the back-end server (running at the port 8000) and a development server (Webpack) for your front-end application. When sending HTTP requests from your front-end application, using the browser's fetch API, the Axios client or the jQuery $.ajax() method (a wrapper for the JavaScript XHR interface), to your back-end API built with Django REST framework the web browser will throw an error related to the Same Origin Policy. Cross Origin Resource Sharing or CORS allows client applications to interface with APIs hosted on different domains by enabling modern web browsers to bypass the Same origin Policy which is enforced by default. CORS enables you to add a set of headers that tell the web browser if it's allowed to send/receive requests from domains other than the one serving the page. You can enable CORS in Django REST framework by using a custom middleware or better yet using the django-cors-headers package Using a Custom Middleware First create a Django application: python manage.py startapp app Next you need to add a middleware file app/cors.py: class CorsMiddleware(object): … -
Handling CORS in Django REST Framework
If you are building applications with Django and modern front-end/JavaScript technologies such as Angular, React or Vue, chances are that you are using two development servers for the back-end server (running at the port 8000) and a development server (Webpack) for your front-end application. When sending HTTP requests from your front-end application, using the browser's fetch API, the Axios client or the jQuery $.ajax() method (a wrapper for the JavaScript XHR interface), to your back-end API built with Django REST framework the web browser will throw an error related to the Same Origin Policy. Cross Origin Resource Sharing or CORS allows client applications to interface with APIs hosted on different domains by enabling modern web browsers to bypass the Same origin Policy which is enforced by default. CORS enables you to add a set of headers that tell the web browser if it's allowed to send/receive requests from domains other than the one serving the page. You can enable CORS in Django REST framework by using a custom middleware or better yet using the django-cors-headers package Using a Custom Middleware First create a Django application: python manage.py startapp app Next you need to add a middleware file app/cors.py: class CorsMiddleware(object): … -
How python's import machinery works
Glossary Let's begin with the basics. From now on when we say the word python we mean python 3.6. Python 3 is the future and (who knows) python 4 will be the future of python 3 etc. Stop using python 2! I'll try to be concise and simple, although the documentation about modules and packages is pretty straight forward. Go ahead and take a look to either learn what these concepts are or just refresh your memory. Python module When we say python module or just module we mean a simple python file, i.e models.py or utils.py. A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. When you put a bunch of modules under a directory then this directory is called a python package. Python package When we say python package or simply just package we mean a directory that contains one or more modules. Packages are a way of structuring Python’s module namespace by using “dotted module names”. Of course, a package may contain a bunch of other directories (packages) which themselves may contain a bunch of other modules mixed with other packages etc. You get … -
Hola, PyCon Colombia!
We (me and Audrey) are going to be giving a keynote speech at PyCon Colombia on February 11th! Hooray! We'll be arriving in Medellin late evening on February 7th and staying a while longer after the conference so we can have the time to explore the lovely city of Medellin. We're very excited, because travel is a rarity for us now and Medellin (and the surrounding area) is supposed to be quite beautiful. Plus, all the Colombians we know online are excellent people - we can't wait to meet them! Our hope is that on the day after PyCon Colombia we can see the sights and eat the foods with Colombians who know Medellin. So let me know if you want to meet up! Hasta pronto! -
Hola, PyCon Colombia!
We (me and Audrey) are going to be giving a keynote speech at PyCon Colombia on February 11th! Hooray! We'll be arriving in Medellin late evening on February 7th and staying a while longer after the conference so we can have the time to explore the lovely city of Medellin. We're very excited, because travel is a rarity for us now and Medellin (and the surrounding area) is supposed to be quite beautiful. Plus, all the Colombians we know online are excellent people - we can't wait to meet them! Our hope is that on the day(s) after PyCon Colombia we can see the sights and eat the foods with Colombians who know Medellin. So let me know if you want to meet up! Hasta pronto! -
Adding JWT Authentication to Python and Django REST Framework Using Auth0
In this tutorial we'll learn how to add JWT authentication to an API built with Django REST framework. Basically we'll use the djangorestframework-jwt package for adding JWT authentication as you would normally do except that we'll change JWT_AUTH to use Auth0. This tutorial assumes you already have a development machine with Python 3 and pip installed and will cover the following points: We'll see how to create a virtual environment, install Django and the other dependencies (Django REST framework and djangorestframework-jwt) We'll see how to create an Auth0 API We'll see how to integrate Auth0 JWT authentication with Django We'll briefly talk about using Auth0 Rules for detecting signup We'll see how to add some Django views for testing JWT We'll see how to use Postman for testing JWT authentication with Auth0 Creating the Django Project So head over to your terminal then create a new virtual environment and activate it using the venv module in your current working directory: python3 -m venv ./myenv source myenv/bin/activate Next install Django using pip: pip install django Now you'll need to create a new Django project using: django-admin startproject auth0-django-example Next create a new application in your project cd auth0-django-example python manage.py startapp … -
QuickTip: Django and AngularJS Conflicting Interpolation Symbols
When using the Django framework with the AngularJS MVC framework for building modern single page applications or SPAs, one of the issues you will encouter is related to both frameworks using the same symbols for template tags i.e { { and } }. So in this quick tip post we'll see how to change the interpolation symbols in AngularJS to avoid these conflicts. Luckliy for us, AngularJS provides the $interpolateProvider provider which allows developers to customize the interpolation symbols which default to { { and } }. Used for configuring the interpolation markup. Defaults to . This feature is sometimes used to mix different markup languages, e.g. to wrap an AngularJS template within a Python Jinja template (or any other template language). Mixing templating languages is very dangerous. The embedding template language will not safely escape AngularJS expressions, so any user-controlled values in the template will cause Cross Site Scripting (XSS) security bugs! -- https://docs.angularjs.org/api/ng/provider/$interpolateProvider Simple AngularJS Example Let's see a simple example: Go ahead and create a base template ng-base.html file in your templates folder then add the following content { % load staticfiles % } <!DOCTYPE html> <html lang="en" ng-app='demoApp'> <head> <base href="/"> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> … -
Adding JWT Authentication to Python and Django REST Framework Using Auth0
In this tutorial we'll learn how to add JWT authentication to an API built with Django REST framework. Basically we'll use the djangorestframework-jwt package for adding JWT authentication as you would normally do except that we'll change JWT_AUTH to use Auth0. This tutorial assumes you already have a development machine with Python 3 and pip installed and will cover the following points: We'll see how to create a virtual environment, install Django and the other dependencies (Django REST framework and djangorestframework-jwt) We'll see how to create an Auth0 API We'll see how to integrate Auth0 JWT authentication with Django We'll briefdly talk about using Auth0 Rules for detecting signup We'll see how to add some Django views for testing JWT We'll see how to use Postman for testing JWT authentication with Auth0 Creating the Django Project So head over to your terminal then create a new virtual environment and activate it using the venv module in your current working directory: python3 -m venv ./myenv source myenv/bin/activate Next install Django using pip: pip install django Now you'll need to create a new Django project using: django-admin startproject auth0-django-example Next create a new application in your project cd auth0-django-example python manage.py startapp … -
QuickTip: Django and AngularJS Conflicting Interpolation Symbols
When using the Django framework with the AngularJS MVC framework for building modern single page applications or SPAs, one of the issues you will encouter is related to both frameworks using the same symbols for template tags i.e { { and } }. So in this quick tip post we'll see how to change the interpolation symbols in AngularJS to avoid these conflicts. Luckliy for us, AngularJS provides the $interpolateProvider provider which allows developers to customize the interpolation symbols which default to { { and } }. Used for configuring the interpolation markup. Defaults to . This feature is sometimes used to mix different markup languages, e.g. to wrap an AngularJS template within a Python Jinja template (or any other template language). Mixing templating languages is very dangerous. The embedding template language will not safely escape AngularJS expressions, so any user-controlled values in the template will cause Cross Site Scripting (XSS) security bugs! -- https://docs.angularjs.org/api/ng/provider/$interpolateProvider Simple AngularJS Example Let's see a simple example: Go ahead and create a base template ng-base.html file in your templates folder then add the following content { % load staticfiles % } <!DOCTYPE html> <html lang="en" ng-app='demoApp'> <head> <base href="/"> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> … -
How to Implement Multiple User Types with Django
This is a very common problem many developers face in the early stages of the development of a new project, and it’s also a question I get asked a lot. So, I thought about sharing my experience with previous Django projects on how to handle multiple user types. I’ve tried many different strategies. In this tutorial I will share my thoughts on this particular topic and share with you the strategies that worked best for me, depending on the requirements of the project. Many of the design decisions depends on the requirements and business model of the application you are developing. I will try to cover as many different scenarios as possible. Read carefully and pick the best option for you. If you learn better with examples or you are in a hurry right now, jump to the practical example. Otherwise, keep reading. Rules of Thumb What you are going to read next is not written in a stone. It’s just some general recommendations that fits most cases. If you have a good reason, or if not following those recommendations will result in a better application design, go ahead and break the “rules”! 1. No matter what strategy you pick, …