简体   繁体   English

/ 'AnonymousUser' 对象的 AttributeError 没有属性 '_meta'

[英]AttributeError at / 'AnonymousUser' object has no attribute '_meta'

I was trying to use signin page but this error shows up.我试图使用登录页面,但出现了这个错误。

Image of error while running in Mozilla web browser localhost在 Mozilla Web 浏览器 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.这是我的聊天机器人应用程序的整个views.py 文件的链接。

https://pastebin.com/2E7mgyuR https://pastebin.com/2E7mgyuR

    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这是我的聊天机器人的models.py

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.如果有人知道任何类型的教程或忘记密码页面的电子邮件验证,请评论链接。

It is because you try to login(request, user) even if authenticate returns None instead of User object.这是因为您尝试login(request, user)即使authenticate返回None而不是User对象。 Simpliest fix is:最简单的解决方法是:

if not user:
    # render/redirect as you please - user=None means that given credentials didn't pass to any User
else:
    login(request, user)

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

相关问题 “ AnonymousUser”对象没有属性“ _meta” - 'AnonymousUser' object has no attribute '_meta' 'AnonymousUser' object 在 Django 中没有属性 '_meta' - 'AnonymousUser' object has no attribute '_meta' in Django 登录期间,“ AnonymousUser”对象没有属性“ _meta” - 'AnonymousUser' object has no attribute '_meta' during log in Django-LoginView:“ AnonymousUser”对象没有属性“ _meta” - Django - LoginView: 'AnonymousUser' object has no attribute '_meta' 'AnonymousUser' 对象没有属性 '_meta' | Django 认证 - 'AnonymousUser' object has no attribute '_meta' | Django Authentication Django 'AnonymousUser' 对象没有属性 '_meta' - Django 'AnonymousUser' object has no attribute '_meta' Django App Engine:AttributeError:'AnonymousUser'对象没有属性'后端' - Django App Engine: AttributeError: 'AnonymousUser' object has no attribute 'backend' 显示用户个人资料出现错误:“ AnonymousUser”对象没有属性“ _meta” - Display User Profile getting ERROR: 'AnonymousUser' object has no attribute '_meta' django 登录表单返回 'AnonymousUser' object 没有属性 '_meta' - django login form returns 'AnonymousUser' object has no attribute '_meta' django 返回 'AnonymousUser' object has no attribute '_meta' 错误 - django returning 'AnonymousUser' object has no attribute '_meta' error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM