奇怪的重定向和会话行为

时间:2010-11-30 17:14:27

标签: django session redirect session-variables

我有一个注册分为几个步骤。当前页面保存在会话中。但我的功能表现得很奇怪。如果我们刷新step1,会话中的变量将更改为2,而不会加载step2。在此步骤中,我正在检查用户是否已经创建,如果没有将step减少为1并重定向到视图,希望将呈现step。但相反,我正在

def my_rte_landing(request):
    step = request.session.get("step", request.REQUEST.get("step", 1))
    logging.debug("my_rte_landing top step: %s" % step)

    if request.method == "POST":
        (... next steps ...)
    else:
        if step == 1:
            logging.debug("step 1")
            html = render_step1(request)
            request.session["step"] = 2      

            return render_to_response('socialauth/login_page.html',{'html': html,}, context_instance=RequestContext(request))            
        else:
            logging.debug("step 2")
            logging.debug("step2, step: %s" % request.session.get('step'))
            new_user = True
            new_user_id = request_user_uid(request, request.user.id)    
            html = render_step2(request)
            request.session["step"] = 3

            return render_to_response('socialauth/login_page.html', 
                                    {'html': html}, context_instance=RequestContext(request))

这是我检查用户是否存在的函数:

def request_user_uid(request, user_id):
    if request.session['step'] == 2:
        logging.debug("1 here")
        id = get_user_id(user_id)
        if id:
            logging.debug("2. here")
            return id

        request.session['step'] = 1
        logging.debug("3. here")
        logging.debug("step: %s" % request.session.get('step'))
    return HttpResponseRedirect(reverse('my_rte_landing'))

我的调试看起来像这样(我第一次访问页面,没有记录,刷新,我可以看到step2,刷新,仍然是第2步)

2010-11-30 17:11:21,434 DEBUG my_rte - nie zalogowany
2010-11-30 17:11:23,245 DEBUG my_rte_landing top step: 1
2010-11-30 17:11:23,246 DEBUG step 1
(first refresh)
2010-11-30 17:11:34,626 DEBUG my_rte_landing top step: 2
2010-11-30 17:11:34,626 DEBUG step 2
2010-11-30 17:11:34,626 DEBUG step2, step: 2
2010-11-30 17:11:34,626 DEBUG 1 here
2010-11-30 17:11:34,628 DEBUG 3. here
2010-11-30 17:11:34,628 DEBUG step: 1
(second refresh)
2010-11-30 17:11:59,523 DEBUG my_rte_landing top step: 3
2010-11-30 17:11:59,523 DEBUG step 2
2010-11-30 17:11:59,524 DEBUG step2, step: 3

2 个答案:

答案 0 :(得分:0)

您重置了步骤..

        new_user_id = request_user_uid(request, request.user.id) 
        # request.session["step"] is currently set to 1 ... however the 
        #  following lines undo that...
        html = render_step2(request)
        request.session["step"] = 3

因此,虽然您可以在request_user_uid()中设置step == 1,但接下来要做的就是将其设置为3 ...

答案 1 :(得分:0)

混合返回值:

def request_user_uid(request, user_id):
    if request.session['step'] == 2:
        logging.debug("1 here")
        id = get_user_id(user_id)
        if id:
            logging.debug("2. here")
            return id #### !!!! returning some value

        request.session['step'] = 1
        logging.debug("3. here")
        logging.debug("step: %s" % request.session.get('step'))
    return HttpResponseRedirect(reverse('my_rte_landing')) #### !!!! HttpResponseRedirect

和功能用法:new_user_id = request_user_uid(request, request.user.id) # contains HttpResponseRedirect instance 这是故意的吗?