Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
The case for a Django upgrade
Like eating Brussels sprouts, everyone knows that keeping software current is good for you. But getting clients to actually upgrade regularly is hard. Why is that? It boils down to this. An upgrade costs money, sometimes a lot of money, but the result has no visible outcome. In fact, in many cases the only outcome is an assurance that you've reduced the probability of attack, intrusion, breach and related unpleasantness. By any measure, that's a tough sales pitch. It's hard to get people to pay for things they can't see. But, like eating your Brussels sprouts, it is important for good health and is part of the cost of doing business online. In an effort to provide believable ammunition to those seeking upgrade funds, we thought it might be helpful to offer some specifics. Although the following is presented through the lens of a website using Python and Django, the principles remain the same for any language or framework. Support for Django 1.8 ends in April 2018. Django is on a consistent release schedule where each third version is a long-term release, meaning it is supported longer than the others. There are plenty of good reasons to upgrade for each … -
Django QuerySet to CSV Files & Datasets
Django QuerySets to CSVs is a ... -
Facebook integration in your website
This post includes how to integrate facebook login in a website using Django. Uses of integrating facebook login: Instead of registration we use facebook login because facebook login takes less time than registration. we can get verified email ids from facebook. Post user actions on userwalls, pages, groups. Send invitations to user friends via logged in user. Send requests from your app to user. The following steps needed for integration. Creating Facebook app. Authenticating user and getting accesstoken. Get user information using accesstoken. Get user friends list. Get user pages. Get user groups. 1.Creating Facebook app: To create facebook app click here and go to apps on top of the page. Click on create new app.The resulting popup box will prompt you to enter 3 things: AppName, Namespace & category. After creating app you will be provided a dash board and grab both the app Id and app secret. Complete the all field in settings basic tab.Click Add Platform at the bottom of the page and select Website.Enter a path where you will want to store your file(i.e redirected url). 2.Authenticating user and getting accesstoken: Authentication flow contains 3 steps. i.Generates a URL asking the user for permission. ii.Facebook returns … -
Understanding Django model formsets in detail and their advanced usage.
Similar to the regular formsets, Django also provides model formset that makes it easy to work with Django models. Django model formsets provide a way to edit or create multiple model instances within a single form. Model Formsets are created by a factory method. The default factory method is modelformset_factory(). It wraps formset factory to model forms. We can also create inlineformset_factory() to edit related objects. inlineformset_factory wraps modelformset_factory to restrict the queryset and set the initial data to the instance’s related objects. Step1: Create model in models.py class User(models.Model): first_name = models.CharField(max_length=150) last_name = models.CharField(max_length=150) user_group = models.ForeignKey(Group) birth_date = models.DateField(blank=True, null=True) Step2: in forms.py from django.forms.models import modelformset_factory from myapp.models import User UserFormSet = modelformset_factory(User, exclude=()) This will create formset which is capable of working with data associated with User model. We can also pass the queryset data to model formset so that it can do the changes to the given queryset only. formset = UserFormSet(queryset=User.objects.filter(first_name__startswith='M')) We can produce an extra form in the template by passing 'extra' argument to the modelformset_factory method, we can use this as follows. UserFormSet = modelformset_factory(User, exclude=(), extra=1) We can customize the form that will be displayed in the … -
How to develop RESTful webservice in Django using Django REST framework?
DjangoRestFramework is widely used to develop restful API. Django REST Framework is easy to use who are familiar with Django, as it is developed on the core concepts of Django. In the current blog post we’ll learn how to develop a RESTful API that performs CRUD operations on the DB. Install Django REST famework using pip command and keep ‘rest_framework’ in your INSTALLED_APPS. Install: pip install djangorestframework In settings.py INSTALLED_APPS = ( ... 'rest_framework', ) Now lets create an application with name api and create a model called ‘Country’ on which we will perform our CRUD operations. In api/models.py from django.db import models class Country(models.Model): name = models.CharField(max_length=10) Add api to your INSTALLED_APPS. INSTALLED_APPS = ( ... 'rest_framework', ‘api’, ) Create migrations with makemigrations and migrate command which creates the tables in your DB. python manage.py makemigrations python manage.py migrate One of the important part of the djangorestframework is the serializers. Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON, XML or other content types. Serializers also provide deserialization, allowing parsed data … -
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!