简体   繁体   English

/ login /'NoneType'对象处的Django1.11 AttributeError没有属性'is_active'

[英]Django1.11 AttributeError at /login/ 'NoneType' object has no attribute 'is_active'

I am writing a login page with Django 1.11. 我正在使用Django 1.11编写登录页面。 Somehow I received this message 我以某种方式收到了此消息

AttributeError at /login/ / login /处的AttributeError

'NoneType' object has no attribute 'is_active' 'NoneType'对象没有属性'is_active'

My code is as below 我的代码如下

def login(request):

    if request.user.is_authenticated():
        return HttpResponseRedirect('/index/')

    username = request.POST.get('username', '')
    password = request.POST.get('password', '')

    user = auth.authenticate(username=username, password=password)

    if user is None and user.is_active:
        auth.login(request, user)
        return HttpResponseRedirect('/index/')
    else:
        return render_to_response('login.html', RequestContext(request, locals()))

And here is my template 这是我的模板

<!doctype html>
<body>
    <form action="" method="post">
        <label for="username">用戶名稱:</label>{% csrf_token %}
        <input type="text" name="username" value="{{username}}" id="username"><br />
        <label for="password">用戶密碼:</label>
        <input type="password" name="password" value="" id="password"><br />
        <input type="submit" value="登入" />
    </form>
</body>

You are trying to do is_active on a None object that's why getting error. 您正在尝试对None对象执行is_active ,这就是为什么会出错。 It should be 它应该是

if user is not None and user.is_active:

So, if user is not none and if it is active then login and send to index page. 因此,如果用户不为空且处于活动状态,则登录并发送到索引页面。

如果存在,则对象返回true,因此您可以更改逻辑

if user and user.is_active

Your logic is broken. 您的逻辑已被破坏。 It should be if user is not None and user.is_axtive . if user is not None and user.is_axtive ,应该是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Flask:AttributeError:&#39;NoneType&#39;对象没有属性&#39;is_active&#39; - Python Flask: AttributeError: 'NoneType' object has no attribute 'is_active' Flask-login AttributeError: &#39;User&#39; 对象没有属性 &#39;is_active&#39; - Flask-login AttributeError: 'User' object has no attribute 'is_active' AttributeError: 'list' object 没有属性 'is_active' - AttributeError: 'list' object has no attribute 'is_active' AttributeError:&#39;SelectQuery&#39;对象没有属性&#39;is_active&#39; - AttributeError: 'SelectQuery' object has no attribute 'is_active' &#39;NoneType&#39;对象没有属性&#39;split&#39;:Django 1.11 - 'NoneType' object has no attribute 'split' : Django 1.11 AttributeError:&#39;dict&#39;对象没有属性&#39;is_active&#39;(PyMongo和Flask) - AttributeError: 'dict' object has no attribute 'is_active' (PyMongo And Flask) AttributeError:“用户”对象没有属性“ is_active”烧瓶应用程序 - AttributeError: 'User' object has no attribute 'is_active' flask application Django - AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;pk&#39; - Django - AttributeError: 'NoneType' object has no attribute 'pk' Django - AttributeError:“NoneType”对象没有属性“方法” - Django - AttributeError: 'NoneType' object has no attribute 'method' Django:AttributeError:&#39;NoneType&#39;对象没有属性&#39;split&#39; - Django: AttributeError: 'NoneType' object has no attribute 'split'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM