Discussion:
[Django] #25203: Document changes to WSGI application loading sequence
Django
2015-07-31 16:07:17 UTC
Permalink
#25203: Document changes to WSGI application loading sequence
------------------------------+--------------------------------------------
Reporter: yscumc | Owner: nobody
Type: | Status: new
Uncategorized |
Component: | Version: 1.7
Uncategorized |
Severity: Normal | Keywords: wsgi setup application loading
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------------
After upgrading Django from 1.6 to 1.7, I found that there were some
undocumented differences in how the wsgi loading is handled.

I had the following in Django 1.6 in my wsgi.py:

{{{
from django.core.wsgi import get_wsgi_application
_application = get_wsgi_application()

# Update DJANGO_SETTINGS_MODULE os env variable from internal Apache env
variable, set by "SetEnv" in httpd.conf
def application(environ, start_response):
if 'DJANGO_SETTINGS_MODULE' in environ:
os.environ['DJANGO_SETTINGS_MODULE'] =
environ['DJANGO_SETTINGS_MODULE']

return _application(environ, start_response)
}}}

This will get the env from the Apache conf and set it before a request to
make sure that the proper settings file is loaded.

However, in Django 1.7, this broke because `django.setup()` is now run as
part of `django.core.wsgi.get_wsgi_application()` and this causes the
settings file to be loaded before the first `application()` call (first
request).

Previously, the loading of the settings file seems to happen with the
first `application()` call, `django.core.wsgi.get_wsgi_application()()`.


Debugging this was actually more difficult than it seems. I initially just
got a 400 BAD REQUEST error after upgrading, with no errors in the logs.
It took a long time to realize my my custom settings file wasn't being
loaded and so `ALLOWED_HOSTS` weren't being loaded either.

After finding out it was a changed behavior in wsgi loading that was
causing the problem, fixing the problem was straightforward, but I
recommend updating the documentation with this change in Django 1.7 so
that others wouldn't run into this issue.


Here's the fixed version that also works with 1.7 (and earlier) if someone
is interested:

{{{
# Update DJANGO_SETTINGS_MODULE os env variable from internal Apache env
variable, set by "SetEnv" in httpd.conf
def application(environ, start_response):
if 'DJANGO_SETTINGS_MODULE' in environ:
os.environ['DJANGO_SETTINGS_MODULE'] =
environ['DJANGO_SETTINGS_MODULE']

# Only attempt to execute get_wsgi_application() once
if not application._app:
application._app = get_wsgi_application()

return application._app(environ, start_response)
application._app = None
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25203>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/049.4e95ad72838b9e647f3101698d9f3c6e%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.
Django
2015-07-31 16:32:58 UTC
Permalink
#25203: Document changes to WSGI application loading sequence in 1.7
-------------------------------------+-------------------------------------
Reporter: yscumc | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Documentation | Version: 1.7
Severity: Normal | Resolution:
Keywords: wsgi setup | Triage Stage: Accepted
application loading |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: => 0
* component: Uncategorized => Documentation
* needs_tests: => 0
* needs_docs: => 0
* type: Uncategorized => Cleanup/optimization
* stage: Unreviewed => Accepted


Comment:

If you could propose a patch, I'll be happy to review it.

--
Ticket URL: <https://code.djangoproject.com/ticket/25203#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/064.3025abbfd004a89625c220b7295d984c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.
Loading...