Django One-Click Application

Django is a high-level Python framework for developing web applications rapidly. DigitalOcean’s Django One-Click Application quickly deploys a preconfigured development environment to a Droplet with Django, Nginx, Gunicorn, and Postgres.

Components

Droplets created using this One-Click have the following software components:

Component Version
Linux Ubuntu 18.04.1
Django Latest from APT
Nginx Latest from APT
Gunicorn Latest from APT
Postgres Latest from APT
Postfix Latest from APT
Certbot Latest from APT

In addition to the package installation, the One-Click also:

  • Enables the UFW firewall to allow only SSH (port 22, rate limited), HTTP (port 80), and HTTPS (port 443) access.

  • Creates the django system user.

  • Configures Nginx for Django.

  • Sets up the Postgres user and database, and syncs the database with Django.

  • Creates the Gunicorn Systemd service file and starts Gunicorn.

Quickstart

After you create a Django One-Click Droplet:

  • The Django user password, Postgres password and secret keys are in /root/.digitalocean_passwords.

  • The Django project is served by Gunicorn, which listens on /home/django/gunicorn.socket.

  • Gunicorn is proxied by Nginx, which listens on port 80. The Nginx configuration file is located at /etc/nginx/sites-enabled/django.

  • Gunicorn is started on boot by a Systemd file at /etc/systemd/system/gunicorn.service.

    This Systemd script also sources a configuration file located at /etc/gunicorn.d/gunicorn.py that sets the number of worker processes. You can find more information on configuring Gunicorn in the Gunicorn project’s documentation.

  • The Django project itself is located at /home/django/django_project.

    If you rename the Django project folder, you will also need to make two configuration file updates: in the Nginx configuration, change the path to your static files; and in the Gunicorn Systemd file, update the WorkingDirectory, name, and pythonpath.

When the Droplet is running, you can immediately start using Django.

Create a Django App

To get started with a basic Django app, log into the server as root. Make sure to substitute the Droplet’s IP address.

ssh root@use_your_droplet_ip

Once you’re connected, switch to the django user, move into the project directory, and create a new app called example. This will create a new directory named example in the folder django_project.

su django
cd /home/django/django_project
python manage.py startapp example

From here, create a view by editing the example/views.py file. For example, using the following code in views.py will tell Django to return Hello, world! This is our first view. as an HTTP response.

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world! This is our first view.")

Next, connect the view you just created to a URL by adding the two highlighted lines below to django_project/urls.py. These lines import the view and sets it to the default URL.

from django.conf.urls import include, url
from django.contrib import admin
from example import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^admin/', include(admin.site.urls)),
]

Log out of the django user and return to the root shell. Then, as root, restart the project.

exit
systemctl restart gunicorn.service

Now, if you reload the Droplet’s IP address in your browser, you’ll see a page with Hello, world! This is our first view.

Manage the Django Project

You can start, restart, or stop the project using the Gunicorn service. For example, to restart the project after making changes, use systemctl restart gunicorn.service.

However, while developing, it can be inconvenient to restart the server every time you make a change. In that case, you might want to use Django’s built-in development server, which automatically detects changes:

systemctl stop gunicorn.service
python manage.py runserver 0.0.0.0:8000

Using the development server, you can access the application through http://use_your_droplet_ip:8000 in a browser. This built-in server does not offer the best performance, so it’s best practice to use the Gunicorn service for production.

Next Steps

You’re ready to start working with Django. From here, you can:

  • Give sudo privileges to your user, lock down root login, and take other steps to make the Droplet ready for production.

  • Use Fabric to automate deployment and other administration tasks.

  • Check out the official Django project documentation.