attributeError at /' anonymoususer'对象没有属性' _Meta'

发布于 2025-02-11 08:07:45 字数 3717 浏览 2 评论 0原文

我试图使用signin页面,但是出现了此错误。

在Mozilla Web浏览器browser localhost运行时的错误图像

=“ https://stackoverflow.com/questions/46284664/django-anonymuser-object-has-no-attribute-meta”> link ,最终将AUTH模型混合到Chatbot模型。

这是我的整个Views.py文件的链接。

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')

我的模型。

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.

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

煮酒 2025-02-18 08:07:45

这是因为您尝试登录(请求,用户)即使authenticate返回none而不是用户 object。最简单的修复是:

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

It is because you try to login(request, user) even if authenticate returns None instead of User object. 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文