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 Comments


  1. Damien Tougas
      September 5, 2011

    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.

    • Bill Tsay
        November 8, 2011

      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

      • Sylvain Hellegouarch
          December 13, 2011

        Hi Bill,

        I apologise for taking so long to approve your comments… WP didn’t notify me for some reason.

    • Krishna
        November 9, 2011

      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

      • Sylvain Hellegouarch
          December 13, 2011

        Hey Krishna,

        Like for Bill, I apologise for taking so long to approve your comment.

  2. Damien Tougas
      December 13, 2011

    I would be happy to share my changes with anyone that would like them. If you are interested, contact me on my blog contact page.

  3. Paul Hartley
      November 6, 2012

    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.

    • Sylvain Hellegouarch
        November 6, 2012

      Great to hear it helped.

  4. Nick Bonfatti
      June 8, 2013

    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?

    • Nick Bonfatti
        June 8, 2013

      forgot to mention the bowser: newest version of chrome.

    • Sylvain Hellegouarch
        June 8, 2013

      Hi, this isn’t the right channel for technical questions. Would you mind using the ws4py mailing list instead?

      In any case, have you tried this code?

      • Nick Bonfatti
          June 8, 2013

        Sorry about that, no problem. I’ll check out that code too.

  5. baxeico
      October 14, 2013

    Thank you, this post was very helpful for a work I described here: http://baxeico.wordpress.com/2013/10/13/django-on-windows