当用户登录时,他被重定向到" / welcome /" url by HttpResponseRedirect。但是我也想返回在它下面注释掉的语句,因为我也希望将字典传递给模板。帮助我使用一种方法来组合这两个语句或任何其他解决方案。
def home(request):
templatename="login.html"
if request.method=="POST":
u=request.POST.get("username")
p=request.POST.get("password")
user=authenticate(username=u, password=p)
if user is not None:
if user.is_active:
print "This User is valid, active and authenticated"
login(request,user)
state = "You're successfully logged in!"
return HttpResponseRedirect("/welcome/")
#return render_to_response(templatename,{'state':state, 'user':user}, context_instance=RequestContext(request))
else:
print "This User is valid but the account has been disabled"
state="This User is valid but the account has been disabled"
else:
print "The Username and Password entered were incorrect"
state = "Your username and/or password were incorrect."
#return render_to_response(templatename,{'state':state, 'user':user}, context_instance=RequestContext(request))
else:
user=None
state=None
return render_to_response(templatename, {'state':state, 'user':user}, context_instance=RequestContext(request))
答案 0 :(得分:5)
您可以使用消息框架.. from django.contrib import messages
views.py
messages.success(request, "You're successfully logged in!")
return HttpResponseRedirect("/welcome/")
另一种选择就是使用会话。但在会话中,您将找到显示后隐藏消息的方法......
#views.py
request.session['state'] = "You're successfully logged in!"
HttpResponseRedirect("/welcome/")
#html
{{ request.session.state }}