index.txt 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. =======================
  2. How to deploy with WSGI
  3. =======================
  4. Django's primary deployment platform is WSGI_, the Python standard for web
  5. servers and applications.
  6. .. _WSGI: http://www.wsgi.org
  7. Django's :djadmin:`startproject` management command sets up a simple default
  8. WSGI configuration for you, which you can tweak as needed for your project, and
  9. direct any WSGI-compliant webserver to use. Django includes getting-started
  10. documentation for the following WSGI servers:
  11. .. toctree::
  12. :maxdepth: 1
  13. modwsgi
  14. gunicorn
  15. uwsgi
  16. The ``application`` object
  17. --------------------------
  18. One key concept of deploying with WSGI is to specify a central ``application``
  19. callable object which the webserver uses to communicate with your code. This is
  20. commonly specified as an object named ``application`` in a Python module
  21. accessible to the server.
  22. .. versionchanged:: 1.4
  23. The :djadmin:`startproject` command creates a :file:`projectname/wsgi.py` that
  24. contains such an application callable.
  25. .. note::
  26. Upgrading from a previous release of Django and don't have a :file:`wsgi.py`
  27. file in your project? You can simply add one to your project's top-level
  28. Python package (probably next to :file:`settings.py` and :file:`urls.py`)
  29. with the contents below. If you want :djadmin:`runserver` to also make use
  30. of this WSGI file, you can also add ``WSGI_APPLICATION =
  31. "mysite.wsgi.application"`` in your settings (replacing ``mysite`` with the
  32. name of your project).
  33. Initially this file contains::
  34. import os
  35. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
  36. # This application object is used by the development server
  37. # as well as any WSGI server configured to use this file.
  38. from django.core.wsgi import get_wsgi_application
  39. application = get_wsgi_application()
  40. The ``os.environ.setdefault`` line just sets the default settings module to
  41. use, if you haven't explicitly set the :envvar:`DJANGO_SETTINGS_MODULE`
  42. environment variable. You'll need to edit this line to replace ``mysite`` with
  43. the name of your project package, so the path to your settings module is
  44. correct.
  45. To apply `WSGI middleware`_ you can simply wrap the application object
  46. in the same file::
  47. from helloworld.wsgi import HelloWorldApplication
  48. application = HelloWorldApplication(application)
  49. You could also replace the Django WSGI application with a custom WSGI
  50. application that later delegates to the Django WSGI application, if you want to
  51. combine a Django application with a WSGI application of another framework.
  52. .. _`WSGI middleware`: http://www.python.org/dev/peps/pep-3333/#middleware-components-that-play-both-sides