在登录表单中显示错误

时间:2016-02-11 21:45:56

标签: python django django-messages

我创建了一个用户登录页面,效果很好!但是当用户输入错误的凭据时,如何在登录页面上显示错误?我尝试使用if和变量,但是无论如何都会显示错误。

在我的观点中,我设置了" error_message"像这样的变量:

def LoginView(request):
    error_message = ''
    logout(request)
    context = RequestContext(request)
    username = request.POST.get('username')
    password = request.POST.get('password')
    user = authenticate(username=username, password=password)
    if user is not None:
        if user.is_active:
            login(request, user)
            return HttpResponseRedirect("/")
        else:
            # Return a 'disabled account' error message
            return HttpResponseRedirect("/")
    else:
        # Return an 'invalid login' error message
  --->  error_message = "Incorrect username or password."
    return render(request, 'profile/login.html', {'error_message': error_message}, context)

但是,在我的模板中,无论如何都会显示此变量,即使我在&#34中设置变量;如果"声明。

这是我的模板:

{% include '__header.html' %}
<style>
main {
    width: 920px;
    background: none;
    margin-top: calc(50vh - 280px)
}
</style>

<div class="form-container">
<form id="user" method="post" action="/login/" 
enctype="multipart/form-data" class="text">
    {% csrf_token %}
    <div class="title">Login to your account</div>
    <div class="form-col">
        <div class="field-container">
            <div class="field-input-container">
                <div class="field-label">Username</div>
                <div class="field-input">
                    <input class="field-input-element" type="text" name="username"/>
                </div>
            </div>
        <div class="field-help"></div>
        </div>
    </div>
    <div class="form-col">
        <div class="field-container" style="height: 110px">
            <div class="field-input-container">
                <div class="field-label">Password</div>
                <div class="field-input">
                <input class="field-input-element" type="password" name="password"/>
                </div>
            </div>
        <div class="field-error">{{ error_message }}</div>
        <div class="field-help"></div>
        </div>
    </div>
    <div class="form-button-container">
    <input type="submit" name="submit" value="Login" class="button form-button"/>
    </div>
</form> 
</div>

{% include '__footer.html' %}

那么,只有在出现错误时如何在模板中显示错误消息?

根据答案更新了代码(仍然做同样的事情)

def LoginView(request):
    template_name='profile/login.html'
    logout(request)
    context = RequestContext(request)

    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return HttpResponseRedirect("/")
            else:
                # Return a 'disabled account' error message
                return HttpResponseRedirect("/")
        else:
            # Return an 'invalid login' error message
            error_message = "Incorrect username or password."
    else:
        error_message = ''

    return render(request, 'profile/login.html', {'error_message': error_message}, context)

3 个答案:

答案 0 :(得分:2)

用于检查用户详细信息的逻辑只有在他们发布表单时才能完成,因此您可以将此部分包装在if request.method == 'POST'

if request.method == 'POST':
    username = request.POST.get('username')
    password = request.POST.get('password')
    user = authenticate(username=username, password=password)
    if user is not None:
        if user.is_active:
            login(request, user)
            return HttpResponseRedirect("/")
        else:
            # Return a 'disabled account' error message
            return HttpResponseRedirect("/")
    else:
        # Return an 'invalid login' error message
        error_message = "Incorrect username or password."

您可能还想将错误div包装在

{% if error_message %}

答案 1 :(得分:1)

您当前的问题是您正在为GET和POST请求设置错误消息。在用户提交数据后,您应该只为POST请求显示它。

if request.method == "POST":
    username = request.POST.get('username')
    password = request.POST.get('password')
    user = authenticate(username=username, password=password)
    if user is not None:
        ...
    else:
        # Return an 'invalid login' error message
        error_message = "Incorrect username or password."

答案 2 :(得分:0)

这是一个可能对您有用的示例,首先导入并在视图中声明模板名称:

from django.contrib import messages

template_name='your_app/login.html' #Inside the login view

然后,登录失败后:

messages.error(request, "incorrect user or password")

最后,渲染模板:

return render(request, self.template_name,{"form_group" : form_group})

希望它有所帮助。