我使用命令提示符运行我的python服务器,其中显示下面的打印语句(如果已到达)。如何在模板login.html
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)
return HttpResponseRedirect("/welcome/")
else:
print("This User is valid but the account has been disabled")
else:
print("The Username and Password entered were incorrect")
else:
user=None
return render_to_response(templatename,{'user': user},RequestContext(request))
答案 0 :(得分:1)
使用而不是
print("This User is valid but the account has been disabled")
此
return HttpResponse("This User is valid but the account has been disabled")
这里是Why
答案 1 :(得分:1)
将您要在模板login.html
上显示的语句存储在变量("州"此处)中,如下所示:
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:
state="This User is valid, active and authenticated"
login(request,user)
return HttpResponseRedirect("/welcome/")
else:
state="This User is valid but the account has been disabled"
else:
state="The Username and Password entered were incorrect"
else:
user=None
return render_to_response(templatename,{'user': user,'state':state},RequestContext(request))
传递给传递给render_to_response()的字典中的状态。
访问login.html
页面中的州{{state}}
你的工作已经完成。