This is Gentoo's testing wiki. It is a non-operational environment and its textual content is outdated.

Please visit our production wiki at https://wiki.gentoo.org

Django

From Gentoo Wiki (test)
Jump to:navigation Jump to:search
This article is a stub. You can help by expanding it.
Resources

Django is a web application framework built on Python.

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 sqlite - embedded 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())"

Sample project

First create the project in the working directory:

user $django-admin startproject myProject

To start development server navigate to the project directory and run:

user $python manage.py runserver

It 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 server for production! It should only be used for development.

Apache modules

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

As of June 16th, 2015 (UTC) dev-python/django-1.7.7 supports 2.7, 3.3 and 3.4 therefore you are free to choose any of them.


Moving on

At this point continue by following this tutorial:

https://docs.djangoproject.com/en/1.8/intro/tutorial01/