如何结合 HttpResponseRedirect & render_to_response?
How to combine HttpResponseRedirect & render_to_response?
当用户登录时,他被 HttpResponseRedirect 重定向到“/welcome/”url。但我也想 return 下面注释掉的语句,因为我也想将字典传递给模板。帮我想个办法把这两个语句或任何其他的解决方案结合起来。
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))
您可以为此使用消息框架.. 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 }}
当用户登录时,他被 HttpResponseRedirect 重定向到“/welcome/”url。但我也想 return 下面注释掉的语句,因为我也想将字典传递给模板。帮我想个办法把这两个语句或任何其他的解决方案结合起来。
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))
您可以为此使用消息框架.. 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 }}