Setting up Django for Production in Django

Learn via video courses
Topics Covered

Overview

In this article, we will take a look at the process of setting up Django for production in Django. Deployment is a crucial stage of project development, a successful deployment indicates that the product is prepared for use by all end users in a real environment and the methods used by developers to deploy the code will impact how fast a product can respond to changes.

A Demo Project

Before getting started with the deployment task, let's create a basic Django project first.

  • Create a virtual environment.
  • Activate the virtual environment.
  • Install the Django package from pip.
  • Create a django project.
  • Initialise a Django application.
  • Create a urls.py file in the application directory and include it in the settings.py file of the project.
  • Include application name in the INSTALLED_APPS
  • Create a folder named templates and include it in the Templates setting configuration of the settings.py file.
  • Create an index.html file with HTML content.
  • Create a view function to render this file.
  • Map this view function to a URL in the urls.py file of the application directory.
  • As a good practice, migrate the changes.
  • Run the server by the following command
  • Route to http://127.0.0.1:8000/ and you will see the index.html file rendered on the screen. home_page

How to Deploy with ASGI?

ASGI stands for Asynchronous Server Gateway Interface. It is termed as WSGI's successor and is designed to act as a common interface for async-capable Python web servers, frameworks, and applications. While WSGI offered a standard for synchronous Python programs, ASGI offers one for both asynchronous and synchronous apps, with WSGI backward compatibility, numerous servers, and application frameworks.

When we run the command python manage.py startproject <project_name>, it creates an asgi.py file in the /<project_name> directory. It establishes a default ASGI configuration with the application created, it can be modified as necessary for your project and instruct any ASGI-compliant application server to utilize.

How to Use Django with Daphne?

Daphne is an ASGI's HTTP and WebSocket protocol server. It was first created to run Django Channels. Additionally, it provides automated protocol negotiation, so URL prefixing is not required to distinguish between HTTP and WebSocket endpoints. It serves as the ASGI reference server and is maintained by Django project members.

To use it with Django, we first need to install it from pip by running the following command

Now as it is installed, we can use daphne to start the server. To invoke it, we need to run the following command in the command line

We need to specify the path of asgi:application file after the keyword daphene. After running this command, one can find the server running on 127.0.0.1:8000.

How to Use Django with Hypercorn?

Based on several Gunicorn-inspired libraries, Hypercorn is an ASGI web server. The specifications for HTTP, WebSockets, and ASGI are supported.

We can install it from pip by running the following command

An ASGI application running the hypercorn command is made available after Hypercorn installation. The location of a module containing an ASGI application object must be provided when calling Hypercorn.

Invoking Hypercorn for a Django app looks like

You can now find the server running on 127.0.0.1:8000.

How to Use Django with Uvicorn?

Uvicorn is an ASGI web server implementation for Python. It is based on both httptools, a Python HTTP parser, and uvloop, a replacement for the native asyncio event loop. Speed is more of a Uvicorn emphasis.

With pip, Uvicorn can be installed by running the following command

There is a uvicorn command that can run ASGI applications once Uvicorn is installed. It is necessary to call Uvicorn with the name of the application and the location of a module containing an ASGI application object, separated by a colon.

The command looks like

Once this command is invoked, we can find the server running on 127.0.0.1:8000.

The Application Object

When we run the command django-admin startproject <project_name> ., this creates a wsgi.py file in the <project_name> directory. The WSGI is an application that helps the code communicates with the server. It is an object generally called by the name application.

When we run the command python manage.py run server, this is Django's built-in server which listens to the wsgi.py` file.

Configuring the Settings Module

Django must import the settings module, which contains a definition of the complete application before the WSGI server can load the application. To find the correct settings module, Django uses the environment variable DJANGO_SETTINGS_MODULE. It needs to include the dotted path to the settings module.

The default path of the wsgi.py is already set in <project_name>.settings, where project_name is the name of your project. This is how the command `python manage.py run server discovers the settings file by default.

Applying WSGI Middleware

In general, middleware is transparent to both the "server/gateway" and the "application/framework" sides of the interface and shouldn't need extra assistance. The application object can be wrapped to apply WSGI middleware. For instance, you may add these lines to wsgi.py at the bottom.

To implement this, we can add the following code at the end of the wsgi.py file

Change the ProjectName and ApplicationName accordingly.

Conclusion

By summarising the article in the following points, we can draw a conclusion.

  • Running the command python manage.py startproject <project_name>, creates an asgi.py file in the /<project_name> directory which establishes a default ASGI configuration.
  • Daphne is an ASGI's HTTP and WebSocket protocol server that was created to run Django channels, and it served as the ASGI server. It is invoked by the keyword daphne providing the path of the asgi application.
  • Hypercorn is another asgi server inspired by Gunicorn-inspired libraries. Same as other methods, it is used to invoke by using the keyword hypercorn along with the path to the asgi application.
  • Uvicorn is a speed-emphasizing ASGI web server built on top of libraries like httptools and uvloop. The name of the application and the location of a module containing an ASGI application object must be passed to Unicorn in order to use it as python -m unicorn myproject.asgi:application