Py学习  »  问与答

django 中间件问题

01071028 • 10 年前 • 9057 次点击  
`class SessionExpiredMiddleware(object):
    def process_request(self,request):
        if 'last_activity' in request.session:
            last_activity = request.session['last_activity']
            now = datetime.datetime.now()

            if (last_activity + datetime.timedelta(minutes=1)) < now:
                if 'user_name' in request.session:
                        del request.session['user_name']
                if 'last_activity' in request.session:
                        del request.session['last_activity']
                error_info = u'登录超时,请重新登录!'
                logging.info('Login session expired!')
                return render_to_response("framework/out_frame.html",{'error_info':error_info})

            if not request.is_ajax():
                # don't set this for ajax requests or else your
                # expired session checks will keep the session from
                # expiring :)
                request.session['last_activity'] = now

        return None`

以上render_to_response为什么会失效?它还是继续执行后面的VIEW呀!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/289
 
9057 次点击  
文章 [ 5 ]  |  最新文章 10 年前
01071028
Reply   •   1 楼
01071028    10 年前

问题终于解决了。谢谢楼上。 我是直接到Django中的HttpResponse中调试出来的。它执行到了HttpResponse。 修改代码:

class SessionExpiredMiddleware(object):
    def process_request(self,request):
        if 'last_activity' in request.session:
            last_activity = request.session['last_activity']
            now = datetime.datetime.now()

            if not request.is_ajax():
                # don't set this for ajax requests or else your
                # expired session checks will keep the session from
                # expiring :)
                # 系统时间AJAX显示不加入考虑
                if (last_activity + datetime.timedelta(minutes=30)) < now:
                    if 'user_name' in request.session:
                        del request.session['user_name']
                    if 'last_activity' in request.session:
                        del request.session['last_activity']
                    error_info = u'登录超时请重新登录'
                    logging.info('Login session expired!')
                    return render_to_response("framework/out_frame.html",{'error_info':error_info})
                else:
                    request.session['last_activity'] = now

        return None

原因是:在页面上我用AJAX做了个显示服务器时间的功能,所以它每秒都会request一次。 超时时,它已经response("framework/out_frame.html“)了。所以后面我再手动执行request,就没啥反应了。现在代码改为上面的,可以达到我的要求了。 只是那个显示时间的AJAX明明response了,还是没显示out_frame.html可能是我其它程序有问题。

谢谢!

Py站长
Reply   •   2 楼
Py站长    10 年前

@01071028 你单步调试一下看看,有没有走到 return HttpResponse(html) 这一步,另外,最好

t = get_template('framework/out_frame.html') 
c = Context({ 'error_info':error_info }) 
html = t.render(c)

也try catch 一下,有可能有异常

01071028
Reply   •   3 楼
01071028    10 年前

删除 • Reply • 3 楼 01071028 0 分钟前

from django.template.loader
import get_template from django.template
import Context from django.http 
import HttpResponse 
t = get_template('framework/out_frame.html') 
c = Context({ 'error_info':error_info }) 
html = t.render(c) 
logging.info(html) 
try: 
    return HttpResponse(html) 
except Exception,e:
    logging.info('fail') 
    logging.info(e)

我换成httpresponse去输出了,没有抛出异常,连‘fail’那行都没执行。 按BeginMan说的我去捕获render_to_response的异常,也同样的结果,捕获不到。 分流又是什么意思呢?我应该怎么去调试?

Py站长
Reply   •   4 楼
Py站长    10 年前

@BeginMan 解决了?没走这一步?

BeginMan
Reply   •   5 楼
BeginMan    10 年前

render_to_response去渲染out_frame.html页面,没走这一步,说明分流了,或抛出异常。在分支语句上做个异常处理。