Serving Django through Werkzeug at a subpath

Written by Johannes on April 25, 2013 Categories: Django, Python, uwsgi, Webdevelopment, Werkzeug

Your ads will be inserted here by

Easy AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

If you want to mix a Django-Application and another WSGI-Application you can serve them through Werkzeug (http://werkzeug.pocoo.org/).

The following snippet serves Django at a subpath and “Hello World” at root.

import os
import sys
 
# save the file at your django project root path for this to work
os.environ["DJANGO_SETTINGS_MODULE"] = "chemocompile.settings"
 
from django.core.wsgi import get_wsgi_application
from werkzeug.wsgi import DispatcherMiddleware
 
def app(environ, start_response):
    status = '200 OK'
    headers = [('Content-type', 'text/plain')]
    start_response(status, headers)
    return ['Hello world']
 
django_project = get_wsgi_application()
 
app = DispatcherMiddleware(app, {
    '/subpath': django_project,
})
 
# this starts the server at port 8080 if the script is run from command line
if __name__ == '__main__':
    from werkzeug.serving import run_simple
    run_simple('', 8080, app)
No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre user="" computer="" escaped="">