Home > Mobile >  AttributeError at / 'AnonymousUser' object has no attribute '_meta'
AttributeError at / 'AnonymousUser' object has no attribute '_meta'

Time:06-28

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.

CodePudding user response:

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)
  • Related