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))
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'),
)
我想如何重定向发送请求的同一个网址,我尝试使用request.path,但它将我重定向到/ accounts / login / url,这是不希望的。
答案 0 :(得分: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
密钥存在(并且可能具有合理的值,以防外部站点直接链接到您的登录页面,或类似的),并且如果这些测试中的任何一个失败(例如可能是用户的个人资料页面或您的网站索引页面),则可以安全地退回。