Python Django1.11 AttributeError at/login/';非类型';对象没有属性';处于活动状态';

Python Django1.11 AttributeError at/login/';非类型';对象没有属性';处于活动状态';,python,django,Python,Django,我正在用Django 1.11编写一个登录页面。不知怎的,我收到了这个消息 AttributeError at/login/ “非类型”对象没有属性“处于活动状态” 我的代码如下 def login(request): if request.user.is_authenticated(): return HttpResponseRedirect('/index/') username = request.POST.get('username', '')

我正在用Django 1.11编写一个登录页面。不知怎的,我收到了这个消息

AttributeError at/login/

“非类型”对象没有属性“处于活动状态”

我的代码如下

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()))
这是我的模板

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

用戶名稱:{%csrf_令牌%}

用戶密碼:

您的逻辑已中断。如果user不是None,并且user.is axctive在无对象上处于活动状态,则应该是
,这就是出错的原因。应该是

if user is not None and user.is_active:

因此,如果用户不是none,并且处于活动状态,则登录并发送到索引页面。

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

if user and user.is_active

如果user为None且user.is\u处于活动状态,则将
更改为
如果user和user.is\u处于活动状态