对于模板上的非活动用户,user.is_authenticated始终返回False [英] user.is_authenticated always returns False for inactive users on template

查看:118
本文介绍了对于模板上的非活动用户,user.is_authenticated始终返回False的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的模板 login.html 中,我有:

{% if form.errors %}
    {% if user.is_authenticated %}
    <div class="alert alert-warning"><center>Your account doesn't have access to this utility.</center></div>
    {% else %}
    <div class="alert alert-warning"><center>Incorrect username or password!</center></div>
    {% endif %}
{% endif %}

我是什么尝试做的是,如果在提交表单后,用户处于非活动状态,则显示另一条错误消息,如果用户未通过身份验证,则显示错误的用户名密码错误消息。这行不通。它始终显示用户名或密码错误!在这两种情况下。但是在视图中,user.is_authenticated即使对于不活动的用户也返回 True

What I am trying to do is, if after form submission, user is inactive then display a different error message and if user is simply not authenticated then display Incorrect username password error message. This doesn't work. It always displays "Incorrect username or password!" in both the cases. However inside a view, user.is_authenticated returns True even for inactive users.

我还有其他方法可以获取完成了吗?我也尝试过

I there any other wway to get this done? I also tried

{% if 'inactive' in form.errors %}

但这也行不通,即使当我尝试打印 form.errors 时,它也会显示

But this doesnt work too, even though when I try to print form.errors, it shows text "This account is inactive" for inactive users.

编辑:
对于视图,我只是在自定义登录视图中使用django的登录视图

For view, I am simply using django's login view inside a custom login view

views.py:

from django.contrib.auth.views import login, logout
from django.shortcuts import render, redirect

def custom_login(request, **kwargs):
    if request.user.is_authenticated():
        return redirect('/homepage/')
    else:
        return login(request, **kwargs)


推荐答案

在登录模板中检查 {%如果user.is_authenticated%} 没有任何意义。如果用户通过了身份验证,则您的 custom_login 视图将把他们重定向到首页。

There isn't any point checking {% if user.is_authenticated %} in your login template. If the user is authenticated, then your custom_login view would have redirected them to the homepage.

如果帐户是无效,则该表单将无效,并且用户将无法登录。该表单的错误如下所示:

If the account is inactive, then the form will be invalid and the user will not be logged in. The forms's errors will look like:

{'__all__': [u'This account is inactive.']}

因此请检查 {%如果form中的'inactive'.errors%} 不起作用,因为错误是使用键 __ all __ 存储的,而不是 inactive

Therefore checking {% if 'inactive' in form.errors %} will not work, because the error is stored with the key __all__, not inactive.

您可以执行 {%,如果此帐户处于非活动状态。 non_field_errors%} ,但这非常脆弱,如果Django更改了非活动用户的错误消息文本,它会损坏。

You could do {% if 'This account is inactive.' in form.non_field_errors %} but this is very fragile, and would break if Django ever changed the text of the error message for inactive users.

最好显示实际错误,而不是尝试找出模板中的错误类型。显示非字段错误的最简单方法是:

It would be better to display the actual errors, rather than trying to find out what sort of error it is in the template. The easiest way to display non-field errors is to include:

{{ form.non_field_errors }}

或者,如果您需要更多控制:

Or, if you need more control:

{% for error in form.non_field_errors %}
    {{ error }}
{% endfor %}

如果您需要为非活动用户更改错误消息,则可以将身份验证表单子类化,然后在登录视图中使用它。

If you need to change the error message for inactive users, you can subclass the authentication form, then use that in your login view.

my_error_messages = AuthenticationForm.error_messages.copy()
my_error_messages['inactive'] = 'My custom message'

class MyAuthenticationForm(AuthenticationForm):
    error_messages = my_error_messages

这篇关于对于模板上的非活动用户,user.is_authenticated始终返回False的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆