attributeError at /' anonymoususer'对象没有属性' _Meta'
我试图使用signin页面,但是出现了此错误。
在Mozilla Web浏览器browser localhost运行时的错误图像
=“ https://stackoverflow.com/questions/46284664/django-anonymuser-object-has-no-attribute-meta”> link ,最终将AUTH模型混合到Chatbot模型。
这是我的整个Views.py文件的链接。
def get(self, request):
return render(request, 'chatbot/signin.html')
def post(self, request):
context = {
'data': request.POST,
'has_error': False
}
username = request.POST.get('username')
password = request.POST.get('pass1')
if username == '':
messages.add_message(request, messages.ERROR,
'Username is required')
context['has_error'] = True
if password == '':
messages.add_message(request, messages.ERROR,
'Password is required')
context['has_error'] = True
user = authenticate(request, username=username, password=password)
# if not user and not context['has_error']:
# messages.add_message(request, messages.ERROR, 'Invalid login')
# context['has_error'] = True
if context['has_error']:
return render(request, 'chatbot/signin.html', status=401, context=context)
login(request, user)
return redirect('chatpage')
我的模型。
from django.db import models
from django.contrib.auth import get_user_model
# Create your models here.
Mod = get_user_model()
class User(Mod):
is_email_verified = models.BooleanField(default=False)
def __str__(self):
return self.email
这就是 在终端显示
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
June 27, 2022 - 09:27:56
Django version 4.0.5, using settings 'Eva_Chatbot.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[27/Jun/2022 09:28:04] "GET / HTTP/1.1" 200 64086
Internal Server Error: /
Traceback (most recent call last):
File "/home/aman/anaconda3/envs/chatbot/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/aman/anaconda3/envs/chatbot/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/aman/anaconda3/envs/chatbot/lib/python3.10/site-packages/django/views/generic/base.py", line 84, in view
return self.dispatch(request, *args, **kwargs)
File "/home/aman/anaconda3/envs/chatbot/lib/python3.10/site-packages/django/views/generic/base.py", line 119, in dispatch
return handler(request, *args, **kwargs)
File "/home/aman/Documents/chatbotEva/Eva_Chatbot/chatbot/views.py", line 141, in post
login(request, user)
File "/home/aman/anaconda3/envs/chatbot/lib/python3.10/site-packages/django/contrib/auth/__init__.py", line 138, in login
request.session[SESSION_KEY] = user._meta.pk.value_to_string(user)
File "/home/aman/anaconda3/envs/chatbot/lib/python3.10/site-packages/django/utils/functional.py", line 259, in inner
return func(self._wrapped, *args)
AttributeError: 'AnonymousUser' object has no attribute '_meta'
[27/Jun/2022 09:28:09] "POST / HTTP/1.1" 500 83583
什么导致此错误?以及如何解决这个问题?
而且,由于某种原因,我尝试解决该错误并以此错误结束,我的电子邮件验证也无法正常工作。如果有人知道此类的任何类型的教程或忘记密码页面的电子邮件验证,请评论链接。
I was trying to use signin page but this error shows up.
Image of error while running in Mozilla web browser localhost
I had tried the solution using this link and ended up mixing auth model to the chatbot model.
this is the link of my whole views.py file of chatbot app.
def get(self, request):
return render(request, 'chatbot/signin.html')
def post(self, request):
context = {
'data': request.POST,
'has_error': False
}
username = request.POST.get('username')
password = request.POST.get('pass1')
if username == '':
messages.add_message(request, messages.ERROR,
'Username is required')
context['has_error'] = True
if password == '':
messages.add_message(request, messages.ERROR,
'Password is required')
context['has_error'] = True
user = authenticate(request, username=username, password=password)
# if not user and not context['has_error']:
# messages.add_message(request, messages.ERROR, 'Invalid login')
# context['has_error'] = True
if context['has_error']:
return render(request, 'chatbot/signin.html', status=401, context=context)
login(request, user)
return redirect('chatpage')
and This is my models.py of chatbot
from django.db import models
from django.contrib.auth import get_user_model
# Create your models here.
Mod = get_user_model()
class User(Mod):
is_email_verified = models.BooleanField(default=False)
def __str__(self):
return self.email
and this is what it is showing in terminal
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
June 27, 2022 - 09:27:56
Django version 4.0.5, using settings 'Eva_Chatbot.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[27/Jun/2022 09:28:04] "GET / HTTP/1.1" 200 64086
Internal Server Error: /
Traceback (most recent call last):
File "/home/aman/anaconda3/envs/chatbot/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/aman/anaconda3/envs/chatbot/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/aman/anaconda3/envs/chatbot/lib/python3.10/site-packages/django/views/generic/base.py", line 84, in view
return self.dispatch(request, *args, **kwargs)
File "/home/aman/anaconda3/envs/chatbot/lib/python3.10/site-packages/django/views/generic/base.py", line 119, in dispatch
return handler(request, *args, **kwargs)
File "/home/aman/Documents/chatbotEva/Eva_Chatbot/chatbot/views.py", line 141, in post
login(request, user)
File "/home/aman/anaconda3/envs/chatbot/lib/python3.10/site-packages/django/contrib/auth/__init__.py", line 138, in login
request.session[SESSION_KEY] = user._meta.pk.value_to_string(user)
File "/home/aman/anaconda3/envs/chatbot/lib/python3.10/site-packages/django/utils/functional.py", line 259, in inner
return func(self._wrapped, *args)
AttributeError: 'AnonymousUser' object has no attribute '_meta'
[27/Jun/2022 09:28:09] "POST / HTTP/1.1" 500 83583
What causing this error? and How to solve this?
and also my email verification is also not working for some reason I had tried solving it and ended with this error. If anybody know any kind of tutorial for this or email verification for forgot password page please comment the link.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您尝试
登录(请求,用户)
即使authenticate
返回none
而不是用户
object。最简单的修复是:It is because you try to
login(request, user)
even ifauthenticate
returnsNone
instead ofUser
object. Simpliest fix is: