开发者

Strange redirect and session behaviour

I have a registration divided into few steps. Current page is saved in session. But my functions behave weird. If we refresh step1, variable in session is changed to 2, and instead of it step2 is loaded. Inside this step I'm checking if the user had been already created, and if not reduce step to 1 and redirect to the view hoping that step will be rendered. But instead I'm getting

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))

here's my function checking for existence of user:

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'))

And my debug looks like this (I'm visiting page for the first time, not logging, refresh, I can see step2, refresh , still step 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


You reset the step ..

        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

So while you might set step == 1 in request_user_uid() the very next thing you do is set it back to 3...


you mix return values:

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

and function usage: new_user_id = request_user_uid(request, request.user.id) # contains HttpResponseRedirect instance is it intentional?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜