Django登录表单错误消息

Ana*_*ova 5 django django-templates django-forms django-views django-login

我已经为我的Django项目创建了登录表单,我遇到了错误消息的问题.这是我在views.py中的函数:

def user_login(request):
if request.method == 'POST':
    username = request.POST.get('username')
    password = request.POST.get('password')

    user = authenticate(username=username, password=password)
    if user:
        if user.is_active:
            login(request, user)
            return HttpResponseRedirect('/friends_plans/users/')
        else:
            return HttpResponse("Your account is disabled")
    else:
        print "Invalid login details: {0}, {1}".format(username, password)
        return HttpResponse("Invalid login details supplied.")
else:
    return render(request, 'friends_plans/index.html', {})
Run Code Online (Sandbox Code Playgroud)

错误消息("提供的登录详细信息无效")出现在新的空白页面上.如果用户名或密码在同一登录页面(index.html)上不正确,您能否告诉我如何显示此消息?

这是我的模板index.html:

{% load staticfiles %}
<html >
<head >
    <title> Friends' Plans </title>
    <meta charset ="utf -8" />
    <link rel="stylesheet" href="{% static 'css/friends_plans.css' %}">
</head >
<body >
    <div id ="container">
        <div id ="header">
            <ul id ="menu">
                <span><a id="firstbutton" href ="" >Friends' Plans</a> </span>
                <span><a id="helpbutton" href ="" >HELP</a></span>
            </ul>
        </div>
        <div id ="left">
            <form id="login_form" method="post" action="">
                {% csrf_token %}

                Username: <input type ="text" name ="username" value=""  size="50" /> <br />
                Password: <input type ="password" name ="password" value=""  size="50"/> <br />
                <input type ="submit" value="submit" />
            </form>
            {% if user.is_authenticated %}
            <a href="/friends_plans/logout/">Logout</a>
            {% else %}
            <a href="/friends_plans/register/">Register here</a><br />
            {% endif %}
        </div>
        <div id ="right">
            <h1 id="welcome">Welcome to Friends' Plans</h1>
            <img class="cat" src={% static 'images/cat4.jpg' %} />
            <img class="cat" src={% static 'images/cat2.jpg' %} />
            <img class="cat" src={% static 'images/cat3.jpg' %} />
            <img class="cat" src={% static 'images/cat6.jpg' %} />
            <img class="cat" src={% static 'images/cat5.jpg' %} />
            <img class="cat" src={% static 'images/cat1.jpg' %} />
        </div>
        <div id ="footer"> Copyright </div>
    </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我有一个想法,设置一个变量,error=False并改变if error=Trueif.is_valid()是否为False,并写入{% if error %}模板,但有一个关于csrf_token的错误,尽管{% csrf_token %}模板中有.

小智 6

在views.py中:

def login_process(request):
    try:
        user = authenticate(username = request.POST['username'],
            password = request.POST['password'])
    except KeyError:
        return render(request, 'friends_plans/index.html',{
            'login_message' : 'Fill in all fields',}) 
    if user is not None:
        #'User' and 'Pass' right
        if user.is_active:
            login(request, user)
        else:
            return render(request, 'friends_plans/index.html',{
                'login_message' : 'The user has been removed',})
    else:
        return render(request, 'friends_plans/index.html',{
            'login_message' : 'Enter the username and password correctly',})
    return HttpResponseRedirect('/friends_plans/users/')
Run Code Online (Sandbox Code Playgroud)

在index.html中:

{% load staticfiles %}
<html >
<head >
    <title> Friends' Plans </title>
    <meta charset ="utf -8" />
    <link rel="stylesheet" href="{% static 'css/friends_plans.css' %}">
</head >
<body >
    <div id ="container">
        <div id ="header">
            <ul id ="menu">
                <span><a id="firstbutton" href ="" >Friends' Plans</a> </span>
                <span><a id="helpbutton" href ="" >HELP</a></span>
            </ul>
        </div>
        <div id ="left">
            <form id="login_form" method="post" action="">
                <div class="add_your_styles">
                    {{ login_message }}
                </div>
                {% csrf_token %}
                Username: <input type ="text" name ="username" value=""  size="50" /> <br />
                Password: <input type ="password" name ="password" value=""  size="50"/> <br />
                <input type ="submit" value="submit" />
            </form>
            {% if user.is_authenticated %}
            <a href="/friends_plans/logout/">Logout</a>
            {% else %}
            <a href="/friends_plans/register/">Register here</a><br />
            {% endif %}
        </div>
        <div id ="right">
            <h1 id="welcome">Welcome to Friends' Plans</h1>
            <img class="cat" src={% static 'images/cat4.jpg' %} />
            <img class="cat" src={% static 'images/cat2.jpg' %} />
            <img class="cat" src={% static 'images/cat3.jpg' %} />
            <img class="cat" src={% static 'images/cat6.jpg' %} />
            <img class="cat" src={% static 'images/cat5.jpg' %} />
            <img class="cat" src={% static 'images/cat1.jpg' %} />
        </div>
        <div id ="footer"> Copyright </div>
    </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

您可以在任何地方放置{{login message}}.它只是一个文本字符串.