如何在html页面上引发注释

时间:2014-08-10 06:06:11

标签: python html django

我使用命令提示符运行我的python服务器,其中显示下面的打印语句(如果已到达)。如何在模板login.html

上显示它们

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)
                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))

2 个答案:

答案 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}} 你的工作已经完成。