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.
Droplets created using this One-Click have the following software components:
|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
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.
After you create a Django One-Click Droplet:
The Django user password, Postgres password and secret keys are in
The Django project is served by Gunicorn, which listens on
Gunicorn is proxied by Nginx, which listens on port
80. The Nginx configuration file is located at
Gunicorn is started on boot by a Systemd file at
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
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
When the Droplet is running, you can immediately start using Django.
To get started with a basic Django app, log into the server as
root. Make sure to substitute the Droplet’s IP address.
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
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.
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.
You’re ready to start working with Django. From here, you can: