Personal tools
You are here: Home Plone XP leocornus.django.ploneproxy A Simple Django Application

A Simple Django Application

— filed under: , , ,

a very simple django application:

  • distribute as egg
  • simple sqllite3 database
  • show a very simple form
  • run on httpd server through mod_python
  • testing PythonAuthenHandler
  • Django Application unit test

setuptools for egg

To distribute a django application as a Python egg, we need setuptools to be installed.

Download the setuptools egg from http://pypi.python.org/pypi/setuptools, and then installed it:

$ sh setuptools-0.6c9-py2.4.egg --prefix=~

Using the --prefix to customize the installation directory.   The option --install-dir will specify the exact directory for installation.  Prefix is the installation folder of your Python.  Make sure the right python installation is in your system PATH if your using the prefix.

For example, I have a testing Python installed under folder /usr/local/rd/python-2.4.6.  The following is how I install the setuptools for my rd python installation:

$ export PATH=/usr/local/rd/python-2.4.6/bin:$PATH
$ sh setuptools-06c9-py2.4.egg --prefix=/usr/local/rd/python-2.4.6

Quick memo for commands to build eggs and distribute eggs:

  • python setup.py --help-commands
    -- show all available commands
  • python setup.py register
    -- upload the egg info to Python package index.  The default one is pypi.python.org.  It could be customized through file ~/.pypirc.
  • python setup.py sdist upload
    -- build the source distribute and upload to Python package index
  • python setup.py bdist_egg upload
    -- build the binary distribute is egg format and upload to Python package index.
  • python setup.py clean install
    -- clean up, build and install

Simple Django Application

With 3 simple Python scripts and leverage on default Django application, we could create our first simple Django Application in 5 minutes.

  • 3 files:
    • manage.py : entry point to powerful Django command tool set.
    • settings.py : settings for the simple Django application: database, root rul, installed app
    • urls.py : define the URLs, only have /admin for now.
  • create the default database and setup super user:
    • $ python leocornus/django/ploneproxy/manage.py syncdb
    • it will also create the super for Django application
  • install this module as a Python egg, otherwise Django could not find the module:
    • $ python setup.py install
  • run the standalone development server:
    • $ python leocornus/django/ploneproxy/manage.py runserver 10.1.1.10:8000

That's it! We have our first Django application running now!

Run Django Application on HTTP

The following is a very simple configuration in httpd.conf to run Django application through mod_python.

LoadModule python_module modules/mod_python.so

<Location /ext>
SetHandler python-program PythonHandler django.core.handlers.modpython SetEnv DJANGO_SETTINGS_MODULE leocornus.django.ploneproxy.settings PythonOption django.root /ext PythonDebug On </Location> Alias /media/ /lib/python/site-packages/django/contrib/admin/media/ <Directory /lib/python/site-packages/django/contrib/adminmedia>
Order deny,allow Allow from all </Directory>

Something tricky:

Apache HTTPD will use the first Python in system PATH to execute the mod_python.  So make sure your Python installation can be found by Apache.

SQLite3 needs write access to the directory containing the DB file because it creates another file "dbfilename-journal" when a transaction is started.  So you need set proper permission on the directory for the user who runs httpd.  Here a ticket about this: http://dev.rubyonrails.org/ticket/285

Unit Testing

We start unit testing for the Django application.

Still pending unit testing for mod_python program!

Shell Console for Django Application

sync database

python mange.py syncdb

clean up the expired sessions

python manage.py cleanup

shell console

python manage.py shell

Tracking History

When Who What Done
2010-05-25 08:17 Sean Chen the product could be distributed as egg and story is complete.
-- 2.0 Hours, 100.0% Done
2010-05-06 08:18 Sean Chen the skeleton for module leocornus.django.ploneproxy, revision r396. the simple setting to run the default django admin application, revision r397
-- 2.0 Hours, 50.0% Done
Document Actions