'WSGIRequest'对象没有属性'session' [英] 'WSGIRequest' object has no attribute 'session'

查看:2765
本文介绍了'WSGIRequest'对象没有属性'session'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在process_response方法中有时在自定义中间件中收到此错误。我有以下列表的中间件:

I get this error sometimes in custom Middleware in process_response method. I have the following list of middlewares:

MIDDLEWARE_CLASSES = [
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.doc.XViewMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'application.middleware.LastCampaignSessionMiddleware'

]

所以会话中间件放在我的中间件之前在源代码中没有任何 del request.session 表达式。
这是process_response方法:

So session middleware is placed before my middleware. I don't have any del request.session expressions in source code. This is process_response method:

    def process_response(self, request, response):
        if 'last_campaign_id' in request.session and request.session['last_campaign_id']:
            if request.COOKIES['last_campaign_id'] != request.session['last_campaign_id']:
                response.set_cookie('last_campaign_id', request.session['last_campaign_id'])
        return response

不知道为什么可以发生在所有。

Not sure why it could happen at all.

编辑03-08-2012 12-30

看起来像浏览器请求的favicon:

It looks like browser requesting favicon:

[03/Aug/2012 10:26:42] "GET /favicon.ico/ HTTP/1.1" 404 6701

django中是否没有默认行为来解析此URL?因为我没有明确指定应该处理这个请求的视图。我不使用页面源代码中的favicon。所以我想这是浏览器请求/favicon.ico。我猜,如果404错误HttpRequest不会正确构造,所以难怪我没有请求对象中的会话。但这只是我的假设。

Is there no default behavior in django to resolve this url? Because I didn't explicitly specify view which should handle this request. And I don't use favicon in page source code. So I guess it's browser who requests /favicon.ico. I guess in case of 404 error HttpRequest wouldn't construct properly so no wonder I have no session in request object. But it's just my assumptions.

如果有必要,我在使用django dev服务器的同时收到此错误。

Also if it necessary I am using django dev server while getting this error.

编辑13 -00

我已经用favicon解决了这个问题,但仍然收到错误。为什么会话可能不存在于请求中?

I have fixed this problem with favicon but still getting error. Why session may not exist in request?

推荐答案

问题在于中间件的顺序。如果要求网址已添加www或尾随/(设置中为APPEND_SLASH,PREPEND_WWW),CommonMiddleware返回HttpResponsePermanentRedirect,则

The problem was in middlewares order.

在这种情况下,django将停止查找process_request方法的中间件列表,并开始运行process_response方法。

CommonMiddleware returns HttpResponsePermanentRedirect in cases when to request url have been added 'www' or trailing '/' (APPEND_SLASH and PREPEND_WWW in settings). In such case django stops looking through middleware list for process_request methods and begins to run process_response methods.

没有关于标准django中间件的这种行为的信息(即中间件可能在某些情况下返回HttpResponse对象)。

It's bad there is no information about such behavior for standard django middlewares (i.e. middleware could return in some cases HttpResponse object).

这篇关于'WSGIRequest'对象没有属性'session'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆