0

视图.py

def login_user(request):
    if request.POST:
        sform = LoginForm(request.POST)
        rform = UserForm()
        if sform.is_valid():
            username = request.POST.get('username')
            password = request.POST.get('password')
            user = authenticate(username = username,password = password)
            if user is not None:
                if user.is_active:
                    login(request,user)
                    return redirect(request.path)
                else:
                    state = "Your account is not active."
                    #or first set the user to is active and then log in 
                    return redirect('acitvate_account',user = user)
                return render_to_response('home.html',{'state':state,'user':user},context_instance = RequestContext(request))
            else:
                state = "Incorrect Username or Password"
                #sform = LoginForm()
                form = LoginForm()
                return render_to_response('register.html',{'state':state,'sform':sform,'rform':rform,'form':form},context_instance = RequestContext(request))
        else:
            state = "Invalid form"
            dLoginForm = LoginForm()
            rform = PartialSignupForm()
            return render_to_response('register.html',{'sform':sform,'rform':rform,'form':form},context_instance = RequestContext(request))
    else:
        dLoginForm = LoginForm()
        rform = PartialSignupForm()
        return render_to_response('category.html',{'dLoginForm':dLoginForm,'rform':rform},context_instance = RequestContext(request))

网址.py

urlpatterns += patterns('beenthere.views',
    #beenthere urls
    (r'^$','index'),
    (r'^home2/$','category'),
    (r'^accounts/login/$','login_user'),
    (r'^accounts/register/$','register_user'),
    (r'^home/(?P<user>\w*)$','home'),
    (r'^logout/','logout_user'),

    )

怀疑

我想如何重定向发送请求的相同 url,我尝试使用 request.path 但它将我重定向到 /accounts/login/ url,这是不需要的。

4

1 回答 1

2

您需要做的是在显示登录表单时跟踪引荐来源网址,可能将该值存储在用户会话中,然后在用户成功登录后将其重定向回该网址。

类似于以下内容:

def login_user(request):
    if request.POST:
        sform = LoginForm(request.POST)
        rform = UserForm()
        if sform.is_valid():
            username = request.POST.get('username')
            password = request.POST.get('password')
            user = authenticate(username = username,password = password)
            if user is not None:
                if user.is_active:
                    login(request,user)
                    return redirect(request.path)
                else:
                    state = "Your account is not active."
                    #or first set the user to is active and then log in 
                    return redirect(req.session['after_login_url'],user = user)
                return render_to_response('home.html',{'state':state,'user':user},context_instance = RequestContext(request))
            else:
                state = "Incorrect Username or Password"
                #sform = LoginForm()
                form = LoginForm()
                return render_to_response('register.html',{'state':state,'sform':sform,'rform':rform,'form':form},context_instance = RequestContext(request))
        else:
            state = "Invalid form"
            dLoginForm = LoginForm()
            rform = PartialSignupForm()
            return render_to_response('register.html',{'sform':sform,'rform':rform,'form':form},context_instance = RequestContext(request))
    else:
        dLoginForm = LoginForm()
        rform = PartialSignupForm()
        req.session['after_login_url'] = req.META.get('HTTP_REFERER')
        return render_to_response('category.html',{'dLoginForm':dLoginForm,'rform':rform},context_instance = RequestContext(request))

当然,您还应该添加检查以确保 HTTP_REFERER 标头存在,并确保after_login_url密钥存在(并且可能具有合理的值,以防外部站点直接链接到您的登录页面或类似页面),并具有如果这些测试中的任何一个失败(例如用户的个人资料页面或您的网站索引页面),则安全后退。

于 2012-08-16T14:20:56.853 回答