<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>How to deploy with WSGI — Django 1.5.9 documentation</title> <link rel="stylesheet" href="../../../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../../../', VERSION: '1.5.9', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../../../_static/jquery.js"></script> <script type="text/javascript" src="../../../_static/underscore.js"></script> <script type="text/javascript" src="../../../_static/doctools.js"></script> <link rel="top" title="Django 1.5.9 documentation" href="../../../index.html" /> <link rel="up" title="Deploying Django" href="../index.html" /> <link rel="next" title="How to use Django with Apache and mod_wsgi" href="modwsgi.html" /> <link rel="prev" title="Deploying Django" href="../index.html" /> <script type="text/javascript" src="../../../templatebuiltins.js"></script> <script type="text/javascript"> (function($) { if (!django_template_builtins) { // templatebuiltins.js missing, do nothing. return; } $(document).ready(function() { // Hyperlink Django template tags and filters var base = "../../../ref/templates/builtins.html"; if (base == "#") { // Special case for builtins.html itself base = ""; } // Tags are keywords, class '.k' $("div.highlight\\-html\\+django span.k").each(function(i, elem) { var tagname = $(elem).text(); if ($.inArray(tagname, django_template_builtins.ttags) != -1) { var fragment = tagname.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>"); } }); // Filters are functions, class '.nf' $("div.highlight\\-html\\+django span.nf").each(function(i, elem) { var filtername = $(elem).text(); if ($.inArray(filtername, django_template_builtins.tfilters) != -1) { var fragment = filtername.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>"); } }); }); })(jQuery); </script> </head> <body> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../../../index.html">Django 1.5.9 documentation</a></h1> <div id="global-nav"> <a title="Home page" href="../../../index.html">Home</a> | <a title="Table of contents" href="../../../contents.html">Table of contents</a> | <a title="Global index" href="../../../genindex.html">Index</a> | <a title="Module index" href="../../../py-modindex.html">Modules</a> </div> <div class="nav"> « <a href="../index.html" title="Deploying Django">previous</a> | <a href="../../index.html" title="&#8220;How-to&#8221; guides" accesskey="U">up</a> | <a href="modwsgi.html" title="How to use Django with Apache and mod_wsgi">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="howto-deployment-wsgi-index"> <div class="section" id="s-how-to-deploy-with-wsgi"> <span id="how-to-deploy-with-wsgi"></span><h1>How to deploy with WSGI<a class="headerlink" href="#how-to-deploy-with-wsgi" title="Permalink to this headline">¶</a></h1> <p>Django’s primary deployment platform is <a class="reference external" href="http://www.wsgi.org">WSGI</a>, the Python standard for web servers and applications.</p> <p>Django’s <a class="reference internal" href="../../../ref/django-admin.html#django-admin-startproject"><tt class="xref std std-djadmin docutils literal"><span class="pre">startproject</span></tt></a> management command sets up a simple default WSGI configuration for you, which you can tweak as needed for your project, and direct any WSGI-compliant webserver to use. Django includes getting-started documentation for the following WSGI servers:</p> <div class="toctree-wrapper compound"> <ul> <li class="toctree-l1"><a class="reference internal" href="modwsgi.html">How to use Django with Apache and mod_wsgi</a></li> <li class="toctree-l1"><a class="reference internal" href="apache-auth.html">Authenticating against Django’s user database from Apache</a></li> <li class="toctree-l1"><a class="reference internal" href="gunicorn.html">How to use Django with Gunicorn</a></li> <li class="toctree-l1"><a class="reference internal" href="uwsgi.html">How to use Django with uWSGI</a></li> </ul> </div> <div class="section" id="s-the-application-object"> <span id="the-application-object"></span><h2>The <tt class="docutils literal"><span class="pre">application</span></tt> object<a class="headerlink" href="#the-application-object" title="Permalink to this headline">¶</a></h2> <p>One key concept of deploying with WSGI is to specify a central <tt class="docutils literal"><span class="pre">application</span></tt> callable object which the webserver uses to communicate with your code. This is commonly specified as an object named <tt class="docutils literal"><span class="pre">application</span></tt> in a Python module accessible to the server.</p> <div class="versionchanged"> <span class="title">Changed in Django 1.4.</span> </div> <p>The <a class="reference internal" href="../../../ref/django-admin.html#django-admin-startproject"><tt class="xref std std-djadmin docutils literal"><span class="pre">startproject</span></tt></a> command creates a <tt class="file docutils literal"><span class="pre">projectname/wsgi.py</span></tt> that contains such an application callable.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Upgrading from a previous release of Django and don’t have a <tt class="file docutils literal"><span class="pre">wsgi.py</span></tt> file in your project? You can simply add one to your project’s top-level Python package (probably next to <tt class="file docutils literal"><span class="pre">settings.py</span></tt> and <tt class="file docutils literal"><span class="pre">urls.py</span></tt>) with the contents below. If you want <a class="reference internal" href="../../../ref/django-admin.html#django-admin-runserver"><tt class="xref std std-djadmin docutils literal"><span class="pre">runserver</span></tt></a> to also make use of this WSGI file, you can also add <tt class="docutils literal"><span class="pre">WSGI_APPLICATION</span> <span class="pre">=</span> <span class="pre">"mysite.wsgi.application"</span></tt> in your settings (replacing <tt class="docutils literal"><span class="pre">mysite</span></tt> with the name of your project).</p> </div> <p>Initially this file contains:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">os</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s">"DJANGO_SETTINGS_MODULE"</span><span class="p">,</span> <span class="s">"mysite.settings"</span><span class="p">)</span> <span class="c"># This application object is used by the development server</span> <span class="c"># as well as any WSGI server configured to use this file.</span> <span class="kn">from</span> <span class="nn">django.core.wsgi</span> <span class="kn">import</span> <span class="n">get_wsgi_application</span> <span class="n">application</span> <span class="o">=</span> <span class="n">get_wsgi_application</span><span class="p">()</span> </pre></div> </div> <p>The <tt class="docutils literal"><span class="pre">os.environ.setdefault</span></tt> line just sets the default settings module to use, if you haven’t explicitly set the <span class="target" id="index-0"></span><a class="reference internal" href="../../../topics/settings.html#envvar-DJANGO_SETTINGS_MODULE"><tt class="xref std std-envvar docutils literal"><span class="pre">DJANGO_SETTINGS_MODULE</span></tt></a> environment variable. You’ll need to edit this line to replace <tt class="docutils literal"><span class="pre">mysite</span></tt> with the name of your project package, so the path to your settings module is correct.</p> <p>To apply <a class="reference external" href="http://www.python.org/dev/peps/pep-3333/#middleware-components-that-play-both-sides">WSGI middleware</a> you can simply wrap the application object in the same file:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">helloworld.wsgi</span> <span class="kn">import</span> <span class="n">HelloWorldApplication</span> <span class="n">application</span> <span class="o">=</span> <span class="n">HelloWorldApplication</span><span class="p">(</span><span class="n">application</span><span class="p">)</span> </pre></div> </div> <p>You could also replace the Django WSGI application with a custom WSGI application that later delegates to the Django WSGI application, if you want to combine a Django application with a WSGI application of another framework.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Some third-party WSGI middleware do not call <tt class="docutils literal"><span class="pre">close</span></tt> on the response object after handling a request — most notably Sentry’s error reporting middleware up to version 2.0.7. In those cases the <a class="reference internal" href="../../../ref/signals.html#django.core.signals.request_finished" title="django.core.signals.request_finished"><tt class="xref py py-data docutils literal"><span class="pre">request_finished</span></tt></a> signal isn’t sent. This can result in idle connections to database and memcache servers.</p> </div> </div> </div> </div> </div> </div> <div class="yui-b" id="sidebar"> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../../../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">How to deploy with WSGI</a><ul> <li><a class="reference internal" href="#the-application-object">The <tt class="docutils literal"><span class="pre">application</span></tt> object</a></li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="../index.html">Deploying Django</a></li> <li>Next: <a href="modwsgi.html">How to use Django with Apache and mod_wsgi</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../../../index.html">Django 1.5.9 documentation</a> <ul><li><a href="../../index.html">“How-to” guides</a> <ul><li><a href="../index.html">Deploying Django</a> <ul><li>How to deploy with WSGI</li></ul> </li></ul></li></ul> </li> </ul> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../../_sources/howto/deployment/wsgi/index.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../../../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Mar 19, 2015</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="../index.html" title="Deploying Django">previous</a> | <a href="../../index.html" title="&#8220;How-to&#8221; guides" accesskey="U">up</a> | <a href="modwsgi.html" title="How to use Django with Apache and mod_wsgi">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>