Proposal: enhance 1.7 migration docs re django.core.handlers.wsgi:WSGIHandler()

824 views
Skip to first unread message

Robert Grant

unread,
Sep 5, 2014, 3:14:31 AM9/5/14
to django-d...@googlegroups.com
I was using

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()


as per whichever This Is The Right Way To Lay Out A Django Application article I was reading at the time. This seems to now break in 1.7 (details - from someone else - here), so I've replaced it with

from django.core.wsgi import get_wsgi_application
from dj_static import Cling

application = Cling(get_wsgi_application())


Which seems to work, although I don't know why.

So:
1) Is this a universal thing everyone should do?
2) If everyone is going to hit this same issue, can we add it to the migration docs?
3) If it's already in the migration docs, apologies :)

Collin Anderson

unread,
Sep 5, 2014, 1:08:37 PM9/5/14
to django-d...@googlegroups.com
I ran into this problem myself upgrading to to django 1.7. The secret is that get_wsgi_application calls django.setup() which is now required to initialize django.

I went through and switched all my code to use get_wsgi_application(). get_wsgi_application() has been in the default wsgi.py in new projects since 1.4. You may alternatively call django.setup() manually, right before you create a WSGIHandler().

The docs mention it for plain python scripts, but don't mention it for wsgi.

Russell Keith-Magee

unread,
Sep 5, 2014, 1:08:58 PM9/5/14
to Django Developers
Hi Robert,

The "old" version breaks because it's now missing a key part of the Django startup sequence:

import django
django.setup()

This is something that was added in 1.7 as part of the app loading changes.

So, to answer your questions directly:

1) You either need to use get_wsgi_application(), or you need to do the call to django.setup() yourself.

2/3) It's kinda documented...

https://docs.djangoproject.com/en/1.7/releases/1.7/#app-loading-changes

... although from that it might not be obvious that this impacts the wsgi script as well. That sounds like it would be a worthwhile addition to the docs.

Yours,
Russ %-)

--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/4a301792-988c-49a2-a86d-f08b20aee132%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aymeric Augustin

unread,
Sep 5, 2014, 2:23:40 PM9/5/14
to django-d...@googlegroups.com
Indeed, that was the recommandation until Django 1.3: https://docs.djangoproject.com/en/1.3/howto/deployment/modwsgi/

And it worked until Django 1.6 but breaks in 1.7. Sorry for missing that. We should add something in the release notes.

Would you mind filing a ticket on https://code.djangoproject.com/?

-- 
Aymeric.



Aymeric Augustin

unread,
Sep 6, 2014, 1:05:53 PM9/6/14
to django-d...@googlegroups.com
I made this addition here: https://github.com/django/django/commit/f8fdb7177b79b79968a3f40612d33c7367ae584f

I’m afraid most projects started before 1.4 will hit this :-/ but the fix is really easy so I haven’t tried to change the code.

-- 
Aymeric.


Marc Tamlyn

unread,
Sep 6, 2014, 1:15:40 PM9/6/14
to django-d...@googlegroups.com
* With 1.4 - wsgi.py didn't exist before 1.4 IIRC


Aymeric Augustin

unread,
Sep 6, 2014, 1:19:36 PM9/6/14
to django-d...@googlegroups.com
wsgi.py didn’t exist in the project template before 1.4, but our mod_wsgi docs told people to create an “apache.wsgi” file containing:
import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Projects that were deployed with WSGI before 1.4 are likely to have kept this code since then, as it worked just fine until 1.7.

-- 
Aymeric.



Robert Grant

unread,
Sep 9, 2014, 8:53:12 AM9/9/14
to django-d...@googlegroups.com
Thanks for this. Sorry I didn't create the PR over the weekend; I didn't have internet access and only got round to looking now.
Reply all
Reply to author
Forward
0 new messages