gunicorn as your Django development server
Contents
Usually when I’m working on a Django project, I use the built-in runserver
command. For deployment, we mostly use nginx
and uwsgi
in front of Django.
However, for a new side project I needed a local Django setup with a faster debug server. To keep things simple, I was looking for a pure Python solution that could serve static files, and did automatic reloading on source change, just like runserver
.
I know that gunicorn is often used for deployment behind a reverse proxy and static file server like nginx
, but I was curious whether gunicorn could also act as Django development server, and also serve up static files without having to run collectstatic
(i.e. serving the static files directly from their development locations).
The answer is: Yes. With DJ-Static serving static files, gunicorn turns out to be a fantastic development server for Django. Here’s how to get it going:
Install gunicorn and DJ-Static in your Django virtual environment:
workon my_ve # I use virtualenvwrapper, hence workon to activate pip install gunicorn dj-static
Add dj_static
to INSTALLED_APPS
in your settings.
In your wsgi.py
, wrap your WSGI application in a Cling
object. The two relevant lines are:
from dj_static import Cling application = Cling(get_wsgi_application())
Now you can start your Django development project as follows:
gunicorn -w 6 --access-logfile - --reload your_project.wsgi:application
This will start six worker processes (tune for your setup), logs access to stderr
, and reloads when it detects changes to the source. Welcome to your shiny, new and slightly faster Django development server!