Django Reference
Contents
Installation
- Download
- cmd> setup.py install
Verify Installation
- python> import django
New Project
- cmd> django-admin.py startproject projectname
Run Project
- cmd> cd projectname
- cmd> python manage.py runserver
- cmd> python manage.py runserver port - for a different port
- Browse to http://localhost:8000/
Database
Configure
- edit settings.py
- mysql> create database dbname
- mysql> create user 'username' identified by 'password'
- mysql> grant all on dbname.* to 'username'
Synchronize
- cmd> python manage.py syncdb
New Application
- cmd> python manage.py startapp appname
Enabling Admin
- edit settings.py, add django.contrib.admin to INSTALLED_APPS
- edit urls.py, uncomment (r'^admin/', include('django.contrib.admin.urls')),
- cmd> python manage.py syncdb
- browse to http://localhost:port/admin/
Controller
- edit settings.py, ensure ROOT_URLCONF = 'urls'
- edit urls.py, add to urlpatterns
(r'^appname/', include('appname.urls')),
- add appname/urls.py and edit to map URL regexps to view names:
from django.conf.urls.defaults import * urlpatterns = patterns('projectname.appname.views', (r'^$', 'index'), #default view (r'^viewname$', 'viewname'), )
View
- edit appname/views.py to add any views defined in the controller
- a viewname is a method defined in views.py
Templates
- edit settings.py and add a absolute directory to contain templates
- add template files to templatedir/appname
- access from your view:
from django.shortcuts import render_to_response def index(request): return render_to_response('appname/index.html', {'key': value})
Special pages
- create a 404.html and 500.html file in templates_root to ensure errors are handled properly when not in debug mode
Model
Deploying
- In settings.py, set DEBUG to False
Apache
- Install mod_python
- Edit httpd.conf, add the lines
LoadModule python_module modules/mod_python.so
<Location "/"> SetHandler python-program PythonHandler django.core.handlers.modpython SetEnv DJANGO_SETTINGS_MODULE projectname.settings PythonPath "['your src path'] + sys.path" PythonDebug On </Location>
<Location "/media/"> SetHandler None </Location>
With "your src path" and "projectname" set appropriately. The media directory is for files to serve directory, i.e. css, image files.
Upgrading
- cmd> svn checkout http://code.djangoproject.com/svn/django/trunk/django/
Webfaction
- The above command should be executed in ~/webapps/django/lib/python2.5