'AnonymousUser' object has no attribute '_meta'

Hi, I’m having problems with the above error. I am new to Django and learning as I go along. Any help will be greatly appreciated.

Views.py:

def loginUser(request):
    if request.user.is_authenticated:
        return redirect('profiles')

    if request.method == 'POST':
        username = request.POST['username'].lower()
        password = request.POST['password']

        try:
            user = User.objects.get(username=username)
        except:
            messages.error(request, 'Username does not exist!')

        user = authenticate(request, username=username, password=password)

        if user is not NONE:
            login(request, user)
            return redirect('profiles')
        else:
            messages.error(request, 'Username or password is incorrect!')

    return render(request, 'users/login.html')

login.html:

{% extends 'main.html' %}

{% block content %}

<h1>You are not logged in!</h1>
<br></br>

<form action="{% url 'login' %}" method="POST">
    {% csrf_token %}
    <input type="text" name="username" placeholder="Username"/>
    <input type="password" name="password" placeholder="Enter Password"/>
    <input type="submit" value="Login"/>
</form>

{% endblock content %}

Full error listing:

Internal Server Error: /login/
Traceback (most recent call last):
  File "/Users/paulbritten/.pyenv/versions/pafdadi/lib/python3.9/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/Users/paulbritten/.pyenv/versions/pafdadi/lib/python3.9/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Volumes/Samsung_T5/MDU_Projects/Django_Projects/PAFDADI_Web_App/pafdadi-webapp/pafdadi/users/views.py", line 26, in loginUser
    login(request, user)
  File "/Users/paulbritten/.pyenv/versions/pafdadi/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 138, in login
    request.session[SESSION_KEY] = user._meta.pk.value_to_string(user)
  File "/Users/paulbritten/.pyenv/versions/pafdadi/lib/python3.9/site-packages/django/utils/functional.py", line 259, in inner
    return func(self._wrapped, *args)
AttributeError: 'AnonymousUser' object has no attribute '_meta'

From reading similar issues on forums I know the issue would appear to be the interaction between:

user = authenticate(request, username=username, password=password)

and

login(request, user)

The only thing is I don’t have enough understanding of these interactions and I can’t make any of the changes required to make my code work.

All the best,

Paul

Can I assume that in your real code you have None and not NONE? (That’s the first thing to check and the only thing I see wrong at the moment.)

If that’s not it, then I’d start with the common debugging techniques of either using the debugger to see what the values are at each step, or tossing some print statements in there to see what is happening at each stage.

Also, you really don’t want to perform a check for the username separate from the password. It becomes a potential vector for attack.

In a real application, you’d also want these entries coming in from a form and not pulling them from request.POST directly. (The example you’re working from is designed to illustrate how to use the authenticate / login combination, not to provide a full example of a login view.)

Hi Ken,

D’oh! Thank you, I overlooked that. Didn’t see the wood from the trees there. Can you point me to a better example for using the authenticate / login combination from a form? Your help is greatly appreciated.

All the best,

Paul

A login form really isn’t any different than any other form.

I typically recommend people start with the Working with Forms docs, in this case paying particular attention to The view.

Your authenticate / logic combination would be handled in the is_valid branch.

Great, thanks again. I’ll look into changing using this approach.