Django

Code

Setting Up mod_python

This is dealt with more in the installation area here: http://www.djangoproject.com/documentation/modpython/.

Troubleshooting mod_python

MD5 Issues

It has been reported that mod_python has trouble returning good MD5 strings. It has been speculated that this is because many apache mods are using the same md5 source (php and so forth), but this is not confirmed. It is likely working in most versions of mod_python, but some have had troubles with it. A workaround is to use SHA strings instead of MD5. A ticket has been submitted (ticket:2249) that would allow one to specify SHA or MD5 session keys to avoid this problem in the future, but as of now, you have to manually update django to make this work.

Note: This problem is more likely because of Debian mucking up their Python installation. See section 'Python MD5 Hash Module Conflict' of 'http://code.google.com/p/modwsgi/wiki/ApplicationIssues' on mod_wsgi site.

How to do the update

  1. Find your django install, it should live inside the site-packages directory of python. On debian this is /usr/lib/python#.#/site-packages. The #'s are your version number. Other OS's may stick python in different places.
  2. You are going to be modifying django/contrib/sessions/models.py and django/contrib/admin/views/decorators.py. Make backups of those two files.
  3. Find the line at the top of the scripts that imports md5. Add sha so that the line looks something like this:
    import base64, md5, sha #there may be a bunch more stuff after this
    
  4. Next, replace all instances of "md5" with "sha" throughout the documents. A search and replace is fine.
    #For example, the following two lines which originally looked like this:
    pickled_md5 = md5.new(pickled + settings.SECRET_KEY).hexdigest()
    return base64.encodestring(pickled + pickled_md5)
    
    #would now look like this:
    pickled_sha = sha.new(pickled + settings.SECRET_KEY).hexdigest()
    return base64.encodestring(pickled + pickled_sha)
    
  5. SHA strings are 40 characters, not 32, we need to fix the string splitting:
    #Find the lines that look like this:
    pickled, tamper_check = encoded_data[:-32], encoded_data[-32:]
    
    #And change them to this:
    pickled, tamper_check = encoded_data[:-40], encoded_data[-40:]
    
  6. Done.

That's all there is to it. Restart apache after saving the files and try to go to your admin area. If you have any additional problems, please head over to the irc channel.

Plesk Issues: Django not being called by Apache on a subdomain

Symptom

Loading the page in a browser shows nothing but the contents of your httpdocs directory (ie: no output from your django project appears).

Probable cause

Apache is not reading your vhost.conf file containing the django handler.

Solution

On a plesk administered system, the conf settings for django to work on a subdomain need to be entered into the vhost.conf file, often located at something like /var/www/vhosts/<yourdomain>/subdomains/<subdomain>/conf/vhost.conf

Open the file, edit it to invoke the django python handler, as described in the Installing Django documentation.

VERY IMPORTANT: Once you are done creating the file, you *must* go back to plesk, open your subdomain settings page, and resave the settings. Until you do this, apache will not load your vhost.conf file.