![](/img/trans.png)
[英]Python Flask: AttributeError: 'NoneType' object has no attribute 'is_active'
[英]Django1.11 AttributeError at /login/ 'NoneType' object has no attribute 'is_active'
我正在使用Django 1.11编写登录页面。 我以某种方式收到了此消息
/ login /处的AttributeError
'NoneType'对象没有属性'is_active'
我的代码如下
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>
您正在尝试对None对象执行is_active
,这就是为什么会出错。 它应该是
if user is not None and user.is_active:
因此,如果用户不为空且处于活动状态,则登录并发送到索引页面。
如果存在,则对象返回true,因此您可以更改逻辑
if user and user.is_active
您的逻辑已被破坏。 if user is not None and user.is_axtive
,应该是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.