From Gentoo Wiki
Jump to:navigation Jump to:search

Django is a web application framework built on Python.

See the Django overview for information on the possibilities this project offers.


USE flags

Cannot load package information. Is the atom dev-python/django correct?


root #emerge --ask dev-python/django

Test if Python's import of django:

user $python -c "import django; print(django.get_version())"

Apache modules

Apache is just one of the setups that can be used, though this is a very popular and mature web server.

When planning on using Apache for production, emerge the WSGI (Web Server Gateway Interface) module:

root #emerge --ask www-apache/mod_wsgi

Enable the WSGI module in Apache's configuration file:

FILE /etc/conf.d/apache2Enabling the WSGI module

Example to set up a given virtual host with WSGI:

FILE /etc/apache2/vhosts.d/00_myProject.confSpecify WSGI config
<VirtualHost *:80>
   WSGIScriptAlias / /var/www/myProject/myProject/

IMPORTANT: For some funny reason the default file (what you should have renamed to, if you follow the example) created by:

user $django-admin startproject myProject

is not properly configured to work with Apache or other severs expect for Django's own. To fix it you must add this line in your file:

FILE /var/www/myProject/myProject/myProject.pyAdd your project to Python's sys.path
sys.path.append('/var/www/myProject') #This is the missing line it must be before os.environ.setdefault(...)


Starting a sample project

As an example, and to make sure everything is working correctly, a sample project may be used. First create the project in the working directory:

user $django-admin startproject myProject

This will create a directory containing files to make up a skeleton Django project, these will be modified according to the project's needs. Move to the project directory:

user $cd myProject

To get a glimpse of Django in action, start the development server by running:

user $python runserver

Open a web browser and navigate to This should only be accessible from the local machine, and not on any other IP addresses, but don't rely on this being secure. The development server will use port 8000 by default; the port can be changed by adding a chosen port number after the runserver command above.

Do not use this development server for production! It should only be used for testing during development.

Production usage

A fully capable web server, such as Apache, must be used for Django projects in production. The Django website has information on some of the setups that can be used.

See the getting started page for links to their excellent tutorial, complete documentation, and more, on the website.