2
當用戶登錄時,他被HttpResponseRedirect重定向到「/ welcome /」url。但是我也想返回下面註釋的語句,因爲我也想將字典傳遞給模板。幫助我一個方法來結合這兩個語句或任何其他解決方案。如何結合HttpResponseRedirect和render_to_response?
Views.py
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))
此消息顯示在哪裏?並且我們不需要返回HttpResponseRedirect? – TheHardRock
[顯示消息](https://docs.djangoproject.com/en/1.7/ref/contrib/messages/#displaying-messages) –