[英]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.