Skip to main content

django-production

Opinionated one-size-fits-most defaults for running Django to production (or any other deployed environment).

Installation

pip install django-production
DJANGO_SETTINGS_MODULE=yourproject.settings django-production-apply

What it does

When you install the package, it will install the following dependencies:

  • whitenoise - for serving static files
  • django-environ - for reading settings from environment variables
  • django-webserver[gunicorn] - for running the webserver via manage.py
  • django-alive - for a health check endpoint at /-/alive/

Running django-production-apply will append the django-production settings to your project's settings file and add the healthcheck endpoint to your project's urlpatterns. You can see the settings that are added in settings.py.

You should add django-production to your requirements to keep the necessary dependencies in place. Alternatively, once the patch is applied, you're free to move the dependencies into your own requirements file and remove django-production altogether.

Running in production

Start the webserver with python manage.py gunicorn.

Set the WEB_CONCURRENCY environment variable to the number of gunicorn workers you want to run. Start with 2x the number of CPU cores.

Required environment variables

  • DJANGO_ENV - set to production to enable production settings
  • SECRET_KEY - a secret key for your project

Optional environment variables when using DJANGO_ENV=production

Under the hood, django-production uses django-environ's FileAwareEnv class to read environment variables. This allows you to append _FILE to any environment variable to load the value from a file. For example, DATABASE_URL_FILE=/var/run/secrets/DATABASE_URL will load the database URL from that file.

Answers

You didn't ask any questions, but if you did, maybe it would be one of these:

Why did you write this?
Django takes an un-opinionated approach to how it should be deployed. This makes it harder for new users. Even experienced users probably copy this from project-to-project. This aims to make it easy to get a project ready to deploy. I also hope it will give us a chance to create some consensus around these settings as a community and maybe start folding some of this into Django itself.

Why are you writing to my settings file? You could just just do an import.

  1. It makes it easier to see the changes. I'm of the opinion that settings files should be as simple as possible. Having the settings right there makes it easier to debug.
  2. A one-size-fits-all approach will never work here. I'm shooting for one-size-fits-most. Users are free to make changes however they see fit once the change is applied. It's basically what startproject is already doing.

I disagree with the settings/packages you're using.
Not a question, but ok. Feel free to submit an issue or pull request with your suggestion and reasoning. We appreciate the feedback and contributions. We may not accept changes that we don't feel fit the spirit of this project (remember, it's opinionated). If you're unsure, don't hesitate to ask.

Publishing a new version

  1. Update the version in django_production/__init__.py
  2. Update the changelog in CHANGELOG.md
  3. Commit the changes
  4. Tag the commit with the version number (git tag -s v0.9.9 -m v0.9.9)
  5. Push the commit and tag (git push && git push --tags)
  6. Publish to PyPI flit publish

To Do

  • Handle media settings for common object stores
  • Email settings including non-SMTP backends like SES

Metadata

Release files for django-production 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-production 0.1.0
File Size Uploaded
django-production-0.1.0.tar.gz 6.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-production 0.1.0
File Interpreter ABI Platform
django_production-0.1.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 12.8 kB

Release files / django-production-0.1.0.tar.gz

Download URL django-production-0.1.0.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
bd7bed2913a566978c8a88f709dd5fd5b8fbf2e34f96d06c3e995b32fa86f38c
BLAKE2b-256 checksum
How to use checksums
5937ce5417c51b2e378cd2fe0432f7451b214309121a386a5fc4eac26fc539b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.28.1

Release files / django_production-0.1.0-py2.py3-none-any.whl

Download URL django_production-0.1.0-py2.py3-none-any.whl
Size 6.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
1e325dcdd8dcd127051f64aacdf6e2843e40fbb4e6fd5831ea5e016e1f0b21db
BLAKE2b-256 checksum
How to use checksums
485bef79a7be4767c95d551e6640723f9993f59bba0871d4d79e5d5f83213d99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.28.1

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page