Update on 2007-12-04. I just saw a post here that says not to use wsgiapp. In particular they say to replace this:

class DjangoApp(object):
    _cp_config = {
        'tools.wsgiapp.on': True,
        'tools.wsgiapp.app': AdminMediaHandler(WSGIHandler()),
}
...
cherrypy.tree.mount(DjangoApp(), '/')

with this:

cherrypy.tree.graft(AdminMediaHandler(WSGIHandler()), '/')

I just tested the suggestion and it works fine, so I have updated the information below. I'm also using Python 2.5 now.

Thanks to The Hand of FuManChu for the information!!!


You want to serve Django content with CherryPy? You've come to the right place.

First, download and install CherryPy. I've been using the trunk build from their SVN repository. So I don't have to remove and re-install it every time I do an update from the repository, I use this method. In the instructions below, adjust your paths accordingly for where you have things installed.

In my ~/projects/python directory, create a folder called cherrypy. Inside that folder create a file called checkout.sh with the following contents:

#!/bin/sh
svn co http://svn.cherrypy.org/trunk

Save your changes and turn on the execute bit (chmod +x checkout.sh). Execute the file (./checkout.sh). You will now have a folder structure like this:

~/projects/python/cherrypy/trunk/cherrypy

Go to your python installation site-packages folder. On the Mac that I am currently writing this from, this is located here:

/Library/Frameworks/Python.framework/Versions/Current/lib/python2.5/site-packages

On my Ubuntu Linux box it is located here:

/usr/lib/python2.5/site-packages

Create a symbolic link to your cherrypy folder:

sudo ln -s ~user_name/projects/python/cherrypy/trunk/cherrypy

(Be sure and replace user_name with your user name!)

Now you have CherryPy available as an installed python module. Verify this as follows:

python
>>> import cherrypy

You shouldn't get any error messages.

OK, now go to the folder that contains your Django project. Create the following files:

pieserver.conf

[global]
server.socket_port = 8080
server.thread_pool = 10
log.screen=True
#log.error_file = "/tmp/cherrypy.error.log"
#log.access_file = "/tmp/cherrypy.access.log"
environment = 'production'

pieserver.py

#!/usr/bin/python
import sys
import os
import cherrypy

from django.core.handlers.wsgi import WSGIHandler
from django.core.servers.basehttp import AdminMediaHandler

os.environ["DJANGO_SETTINGS_MODULE"] = "YOURSITE.settings"

class DjangoApp(object):
    django_conf = { 
        '/YOURAPP/media' : {
            'tools.staticdir.on' : True,
            'tools.staticdir.root' : os.path.abspath(os.path.join(os.path.dirname(__file__),'YOURSITE','YOURAPP')),
            'tools.staticdir.dir' : 'media',
        }
    }


if __name__ == '__main__':
    sys.path.insert(0,"..")
    conf = os.path.join(os.path.dirname(__file__), 'pieserver.conf')
    cherrypy.config.update(conf)
    cherrypy.tree.graft(AdminMediaHandler(WSGIHandler()), '/')
    cherrypy.server.quickstart()
    cherrypy.engine.start()

Notes:

  1. Replace YOURSITE with the name of your Django site
  2. Replace YOURAPP with the name of your application within the YOURSITE directory.

In the example above I'm assuming that you want to serve static media content from anything in YOURAPP that uses this URL:

/YOURAPP/media/...

Now to run it, just do this: python pieserver.py

Easy!


Page last modified on December 04, 2007, at 07:42 AM