Django framework

The Django extension streamlines the process of building Django application rocks.

It facilitates the installation of Django application dependencies, including Gunicorn, inside the rock. Additionally, it transfers your project files to /django/app within the rock (/app on Ubuntu 26.04 LTS). By default, the system foundation, or base, is set as bare to generate a lightweight image.

Note

The Django extension is compatible with the bare, ubuntu@22.04, ubuntu@24.04 and ubuntu@26.04 bases. Support for Ubuntu 26.04 LTS is experimental.

Part names added by the extension vary by Ubuntu version. On Ubuntu 22.04 LTS and Ubuntu 24.04 LTS, write extension/part. On Ubuntu 26.04 LTS, write extension.part. For rocks with base: bare, follow the convention for the configured build-base.

The Django extension supports both synchronous and asynchronous Gunicorn workers.

Use the extension

Declare the extension in rockcraft.yaml:

rockcraft.yaml
extensions:
  - django-framework

You can generate this project file by running rockcraft init --profile django-framework in the application’s directory. To inspect the parts, services, and other configuration contributed by the extension, run rockcraft expand-extensions in the same directory as the rockcraft.yaml file.

Project requirements

There are 2 requirements to be able to use the django-framework extension:

  1. There must be a requirements.txt file in the root directory of the project with Django declared as a dependency. On Ubuntu 26.04 LTS, you can use a uv project instead.

  2. The Django project directory at the rock root must be named the same as the name in the project file, with any - replaced by _. The wsgi.py file must be at either <rock-name>/<rock-name>/wsgi.py or <rock-name>/mysite/wsgi.py and define an application callable. If both files exist, <rock-name>/<rock-name>/wsgi.py takes precedence.

For the project to make use of asynchronous Gunicorn workers:

  • The requirements.txt or pyproject.toml file must include gevent as a dependency.

uv projects

The extension doesn’t support uv projects on these bases. It builds the application with the Python plugin and installs dependencies from requirements.txt.

If both a uv.lock and a pyproject.toml file are present in the project root, the extension builds the application with the uv plugin instead of the Python plugin. It installs dependencies from the lockfile with uv sync. Gunicorn is injected after the build step regardless of the lockfile contents. In this case, a requirements.txt file is not required.

The uv plugin requires both files, meaning the application will fail to pack if uv.lock is present but pyproject.toml is missing. If uv.lock is absent, the extension builds with the Python plugin and still requires requirements.txt; a pyproject.toml file alone is not enough.

App dependencies

The stage-packages key specifies all additional dependencies. If the Django app has its own special dependencies, this key must declare them.

The following example specifies the libpq-dev package. For rocks built on the bare base, list slices instead. Instead of libpq-dev, for example, you’d list libpq5_libs.

rockcraft.yaml
parts:
  django-framework/dependencies:
    stage-packages:
      # list required packages or slices for your Django application below.
      - libpq-dev
rockcraft.yaml
parts:
  django-framework.dependencies:
    stage-packages:
      # list required packages or slices for your Django application below.
      - libpq-dev

StatsD exporter

A StatsD exporter is installed alongside the Gunicorn server to record server metrics. Some of the Gunicorn-provided metrics are mapped to new names:

Gunicorn metric

StatsD metric

gunicorn.request.status.*

django_response_code

gunicorn.requests

django_requests

gunicorn.request.duration

django_request_duration

The exporter listens on localhost at port 9125. You can push your own metrics to the exporter using any StatsD client. This snippet from an example Django app uses pystatsd as a client:

import statsd
c = statsd.StatsClient('localhost', 9125)
c.incr('my_counter')

See the StatsD exporter documentation for more information.

Gunicorn worker selection

If gevent is listed as a dependency in either the requirements.txt or pyproject.toml file, Rockcraft sets the Gunicorn worker to gevent in the Pebble plan. Otherwise, it uses sync.

When the project instead needs synchronous workers, you can override the worker type by adding --args django sync to the Docker command that launches the rock:

docker run --name django-container -d -p 8000:8000 django-image:1.0 \
--args django sync