Django

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.

Installation

USE flags

USE flags for dev-python/django High-level Python web framework

doc Add extra documentation (API, Javadoc, etc). It is recommended to enable per package instead of globally
sqlite Add support for sqliteembedded sql database
test Enable dependencies and/or preparations necessary to run tests (usually controlled by FEATURES=test but can be toggled independently)
verify-sig Verify upstream signatures on distfiles

Emerge

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
APACHE2_OPTS="... -D WSGI"

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/myProject.py
   ...
</VirtualHost>

IMPORTANT: For some funny reason the default wsgi.py file (what you should have renamed to myProject.py, 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 wsgi.py 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(...)
...

Usage

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 manage.py runserver

Open a web browser and navigate to http://127.0.0.1:8000. 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.

Important
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.