Hosting a Django application on a CherryPy server

Recently at work I’ve had the requirement to host a Django application in a CherryPy server. I first looked for various projects I knew were doing just that. Unfortunately, after trying them I was rather disapointed. Their approach is to provide a command similar to the famous Django runserver‘s one but I’ve found it to be more complex than necessary. So I wrote my own module that performs those operations by staying much closer to how CherryPy does work, most specifically by using the process bus coming with CherryPy.

I’m sharing a stripped down version of the module I wrote which shows how one could host a Django application in a CherryPy server. Hopefully this might help some of you.

You can find the code along side a minimal Django application showing how this works here (BSD licence). I used Django 1.3 to generate a default project but the code above works well with older version of Django.

Edit 16/03/2012: Thanks to Damien Tougas, I’ve wrapped up a better recipe for hosting a Django application into a CherryPy application server.

13 thoughts on “Hosting a Django application on a CherryPy server”

  1. Thanks for this. I took your script and made some changes that you may (or may not) be interested in. Namely, a more elegant way to load the Django settings, and secondly, the static information is loaded from the Django config rather than being hard-coded. If you send me an email I would be happy to send you a copy.

    1. Hi Damien,

      Wonder if you can email me the copy of your modified code? We have a need to embed django into cherrypy too.

      Thanks.

      Bill

    2. Damien, We are trying to the exact thing – run CherryPy and Django apps in the on the CherryPy server. Our need is to be able to share the session and auth done in CherryPy with apps being developed in Django.

      Can you share the changes you made?

      Thanks
      Krishna

  2. I am looking for a way to avoid using apache for my django application. The traffic will be small so I will try this cherrypy solution.
    The three .py files on Lawouach work once the name of the application is changed.
    Not sure why the main .py file is __init__.py – I renamed it!
    Thank you Sylvain and Damien for this project.

  3. I can’t get this to work. It starts up OK when I rename __init__.py to __main__.py, and run python . in the folder, but when i connect to the website, the chat window says Connection closed by server: 1006 "" and if I try sending output, it goes to the console but doesn’t show up in the window. I pip installed django, cherrypy, and ws4py. Is there something I’m missing?

Comments are closed.