Using DjangoApplication

49 views
Skip to first unread message

Kyle Adams

unread,
Aug 20, 2015, 9:21:25 AM8/20/15
to 2degrees FLOSS
I'm getting this error: "AttributeError: 'WSGIRequest' object has no attribute 'copy'"

I see that it's because Django's WSGIRequest is being passed in, instead of the TwodWSGIRequest. I've looked over the docs at http://pythonhosted.org/twod.wsgi/request-objects.html and I'm still not sure how to get Django to use the extended WSGI app. I think part of the problem here is that I have very little experience with Django, so apologies in advance.

The docs say:

"When you use this application, your views receive the request as an instance of TwodWSGIRequest automatically."

OK, great, how do I use the application?

I *think* the answer is below, in the "Using the WSGI application directly" section, in this bit of code:

----
from os import environ
environ['DJANGO_SETTINGS_MODULE'] = "yourpackage.settings"

from twod.wsgi import DjangoApplication
----

That said, as someone with no Django experience, I have no idea where this code should go. The docs state, "you can import it as you would normally do in Django", but I have no idea what I would "normally do in Django". Help?

Thanks,
Kyle

Kyle Adams

unread,
Aug 20, 2015, 9:40:18 AM8/20/15
to 2degrees FLOSS
More information…

I'm embedding my code (a Pyramid app) inside a third party Django, so it's the reverse of the use cases mentioned in the docs (embedding third party WSGI apps in your Django app). That means I can't really change the Django code.

I ended up using call_wsgi_app in my view (which is loaded into the Django app via a plugin mechanism it has):

def jujugui(request, path_info):
    # Enhance Django's WSGIRequest by wrapping it in TwodWSGIRequest.
    req = TwodWSGIRequest(request.environ)
    app = MyApp()
    return call_wsgi_app(app, req, path_info)

The problem I'm running into now is when call_wsgi_app tries to copy the request, at line 47 in embedded_wsgi.py:

new_request = request.copy()

It fails with an "AttributeError: can't set attribute". The underlying problem is that, while webob.Request.body is mutable, HttpRequest.body is not. So when WebOb calls "self.body = b''" (webob/request.py, line 956) on my TwodWSGIRequest instance, it's trying to set the HttpRequest.body and that fails. Right now I'm kinda at an impasse. Not sure how this should work?

Kyle

gustavonarea

unread,
Aug 21, 2015, 1:00:49 PM8/21/15
to 2degrees FLOSS
Hi, Kyle.


On Thursday, August 20, 2015 at 2:21:25 PM UTC+1, Kyle Adams wrote:
I'm getting this error: "AttributeError: 'WSGIRequest' object has no attribute 'copy'"

I see that it's because Django's WSGIRequest is being passed in, instead of the TwodWSGIRequest. I've looked over the docs at http://pythonhosted.org/twod.wsgi/request-objects.html and I'm still not sure how to get Django to use the extended WSGI app. I think part of the problem here is that I have very little experience with Django, so apologies in advance.

The docs say:

"When you use this application, your views receive the request as an instance of TwodWSGIRequest automatically."

OK, great, how do I use the application?

That's a good point. I mistakenly assumed familiarity with this part of Django when I wrote the docs.

The answer depends on the version of Django that you're using.  If you're using a relatively recent version, "django-admin startproject" must've created a file called "wsgi.py". That file contains a callable by the name of "application", which you should change to be an instance of TwodWSGIRequest.

I'll update the docs to make this clear.

 

I *think* the answer is below, in the "Using the WSGI application directly" section, in this bit of code:

----
from os import environ
environ['DJANGO_SETTINGS_MODULE'] = "yourpackage.settings"

from twod.wsgi import DjangoApplication
----

That said, as someone with no Django experience, I have no idea where this code should go. The docs state, "you can import it as you would normally do in Django", but I have no idea what I would "normally do in Django". Help?


The bit about the import refers to DjangoApplication, which is the last import in your quote. Alternatively, you could use django.core.wsgi.get_wsgi_application() to get the WSGI app for Django. The two options achieve the same, as long as you've set the setting WSGI_APPLICATION.

Cheers.

gustavonarea

unread,
Aug 21, 2015, 1:02:47 PM8/21/15
to 2degrees FLOSS
Regarding this issue, which version of WebOb are you using? We're not yet supporting v1:

Cheers.

Kyle Adams

unread,
Aug 24, 2015, 7:25:34 AM8/24/15
to 2degrees FLOSS
On Friday, August 21, 2015 at 1:02:47 PM UTC-4, gustavonarea wrote:
Regarding this issue, which version of WebOb are you using? We're not yet supporting v1:


Ah, yes, I'm using Django 1.7.10 and WebOb 1.4.1. I ended up monkeypatching my way around the problem for the time being.

Back to the DjangoApplication:

Since I'm embedding my code (the WSGI app) in a third party Django app, I want to minimize (or avoid altogether if possible) changing any of that third party code. That said, the Django app does have a mechanism for plugins (which my WSGI app is) to control settings. If I'm understanding correctly, I just need to set WSGI_APPLICATION to 'twod.wsgi.handler.DjangoApplication'?

Thanks,
Kyle

Gustavo Narea

unread,
Aug 24, 2015, 12:22:38 PM8/24/15
to 2degree...@googlegroups.com
Hi, Kyle.

On Mon, Aug 24, 2015 at 12:25 PM, Kyle Adams <kada...@gmail.com> wrote:
 
If I'm understanding correctly, I just need to set WSGI_APPLICATION to 'twod.wsgi.handler.DjangoApplication'?

That setting should point to an instance of that class, not the class itself. By default, Django assumes it is "<your-package>.wsgi.application", where "<your-package>.wsgi" is a module created by Django.

HTH,

 - Gustavo.
 

Thanks,
Kyle

--
You received this message because you are subscribed to the Google Groups "2degrees FLOSS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to 2degrees-flos...@googlegroups.com.
To post to this group, send email to 2degree...@googlegroups.com.
Visit this group at http://groups.google.com/group/2degrees-floss.
For more options, visit https://groups.google.com/d/optout.



--
Gustavo Narea.
Software Developer.
Reply all
Reply to author
Forward
0 new messages