Buildout recipe for Django

pip install redsolution.djangorecipe==1.1.2


Djangorecipe: easy install of Django with buildout

With djangorecipe you can manage your django site in a way that is familiar to buildout users. For example:

  • bin/django to run django instead of bin/python
  • bin/test to run tests instead of bin/python test yourproject. (Including running coverage "around" your test).
  • bin/django automatically uses the right django settings. So you can have a development.cfg buildout config and a production.cfg, each telling djangorecipe to use a different django settings module. bin/django will use the right setting automatically, no need to set an environment variable.

Djangorecipe is developed on github at, you can submit bug reports there. It is tested with travis-ci and the code quality is checked via Code Health


You can see an example of how to use the recipe below with some of the most common settings:

show-picked-versions = true
parts =
eggs =
develop = .
# ^^^ Assumption: the current directory is where you develop 'yourproject'.
versions = versions

Django = 1.8.2
gunicorn = 19.3.0

recipe = djangorecipe
settings = development
eggs = ${buildout:eggs}
project = yourproject
test = yourproject
scripts-with-settings = gunicorn
# ^^^ This line generates a bin/gunicorn-with-settings script with
# the correct django environment settings variable already set.

Earlier versions of djangorecipe used to create a project structure for you, if you wanted it to. Django itself generates good project structures now. Just run bin/django startproject <projectname>. The main directory created is the one where you should place your buildout and probably a

Startproject creates a script for you. You can remove it, as the bin/django script that djangorecipe creates is the (almost exact) replacement for it.

See django's documentation for startproject.

You can also look at cookiecutter.

Supported options

The recipe supports the following options.

This option sets the name for your project.
You can set the name of the settings file which is to be used with this option. This is useful if you want to have a different production setup from your development setup. It defaults to development.
If you want a script in the bin folder to run all the tests for a specific set of apps this is the option you would use. Set this to the list of app labels which you want to be tested. Normally, it is recommended that you use this option and set it to your project's name.

Script names you add to here (like 'gunicorn') get a duplicate script created with '-with-settings' after it (so: bin/gunicorn-with-settings). They get the settings environment variable set. At the moment, it is mostly useful for gunicorn, which cannot be run from within the django process anymore. So the script must already be passed the correct settings environment variable.

Note: the package the script is in must be in the "eggs" option of your part. So if you use gunicorn, add it there (or add it as a dependency of your project).

Like most buildout recipes, you can/must pass the eggs (=python packages) you want to be available here. Often you'll have a list in the [buildout] part and re-use it here by saying ${buildout:eggs}.
If you set coverage = true, bin/test will start coverage recording before django starts. The coverage library must be importable. See the extra coverage notes further below.

The options below are for older projects or special cases mostly:

Use this option to specify a custom settings path to be used. By default, the project and settings option values are concatenated, so for instance myproject.development. dotted-settings-path = somewhere.else.production allows you to customize it.
All paths specified here will be used to extend the default Python path for the bin/* scripts. Use this if you have code somewhere without a proper
The name of the script created in the bin folder. This script is the equivalent of the Django normally creates. By default it uses the name of the section (the part between the [ ]). Traditionally, the part is called [django].
Specify some Python initialization code to be inserted into the control-script. This functionality is very limited. In particular, be aware that leading whitespace is stripped from the code given.
An extra script is generated in the bin folder when this is set to true. This is mostly only useful when deploying with apache's mod_wsgi. The name of the script is the same as the control script, but with .wsgi appended. So often it will be bin/django.wsgi.
Use this option if you need to overwrite the name of the script above.
In the wsgi deployment script, you sometimes need to wrap the application in a custom wrapper for some cloud providers. This setting allows extra content to be appended to the end of the wsgi script. For instance application = some_extra_wrapper(application). The limits described above for initialization also apply here.
This is the name of the testrunner which will be created. It defaults to test.

Coverage notes

Starting in django 1.7, you cannot use a custom test runner (like django-nose) anymore to automatically run your tests with coverage enabled. The new app initialization mechanism already loads your, for instance, before the test runner gets called. So your shows up as largely untested.

With coverage = true, bin/test starts coverage recording before django gets called. It also prints out a report and export xml results (for recording test results in Jenkins, for instance) and html results.

Behind the scenes, true is translated to a default of report xml_report html_report. These space-separated function names are called in turn on the coverage instance. See the coverage API docs for the available functions. If you only want a quick report and xml output, you can set coverage = report xml_report instead.

Note that you cannot pass options to these functions, like html output location. For that, add a .coveragerc next to your buildout.cfg. See the coverage configuration file docs. Here is an example:

omit =
source = your_app

show_missing = true

directory = htmlcov

output = coverage.xml

Example configuration for mod_wsgi

If you want to deploy a project using mod_wsgi you could use this example as a starting point:

<Directory /path/to/buildout>
     Order deny,allow
     Allow from all
     ServerName      my.rocking.server
     CustomLog       /var/log/apache2/my.rocking.server/access.log combined
     ErrorLog        /var/log/apache2/my.rocking.server/error.log
     WSGIScriptAlias / /path/to/buildout/bin/django.wsgi

Corner case: there is a problem when several wsgi scripts are combined in a single virtual host instance of Apache. This is due to the fact that Django uses the environment variable DJANGO_SETTINGS_MODULE. This variable gets set once when the first wsgi script loads. The rest of the wsgi scripts will fail, because they need a different settings modules. However the environment variable DJANGO_SETTINGS_MODULE is only set once. The new initialization option that has been added to djangorecipe can be used to remedy this problem as shown below:

settings = acceptance
initialization =
    import os
    os.environ['DJANGO_SETTINGS_MODULE'] = '${django:project}.${django:settings}'

Generating a control script for PyDev

Running Django with auto-reload in PyDev requires adding a small snippet of code:

import pydevd
pydevd.patch_django_autoreload(patch_remote_debugger=False, patch_show_console=True)

just before the if __name__ == "__main__": in the module (or in this case the control script, normally bin/django, that is generated). The following example buildout generates two control scripts: one for command-line usage and one for PyDev, with the required snippet, using the recipe's initialization option:

parts = django pydev
eggs =

recipe = djangorecipe
eggs = ${buildout:eggs}
project = dummyshop

<= django
initialization =
    import pydevd
    pydevd.patch_django_autoreload(patch_remote_debugger=False, patch_show_console=True)

Example usage of django-configurations

django-configurations ( is an application that helps you organize your Django settings into classes. Using it requires modifying the file. This is done easily using the recipe's initialization option:

parts = django
eggs =

recipe = djangorecipe
eggs = ${buildout:eggs}
project = myproject
initialization =
    # Patch the manage file for django-configurations
    import os
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
    os.environ.setdefault('DJANGO_CONFIGURATION', 'Development')
    from import execute_from_command_line
    import django = execute_from_command_line