Search code examples
djangodjango-authentication

Can not log in with Django, Attribute Error: AnonymousUser' object has no attribute '_meta'


I am trying to sigin my user in, the error I keep getting is an AttributeError: AnonymousUser' object has no attribute '_meta'.

I am following the documentation from Django on how to log a user in, have tried that but I keep getting the error.

https://docs.djangoproject.com/en/2.2/topics/auth/default/#django.contrib.auth.login

view.py

def signin(request):
    if request.method == 'GET':
        return render(request, 'auth/signin.html', { })

    if request.method == 'POST':
        form = SigninForm(request.POST)

        if form.is_valid():
            username = request.POST["username"]
            password = request.POST["password"]
            user = authenticate(request, username=username, password=password)
            if user is None:
                login(request, user)
                return HttpResponseRedirect('/')
            else:
                return HttpResponse('Invalid Credientials', status=401 )
        else:
            return HttpResponse("Form is not valid", status=401)

What I want is the user to signin but I keep getting the attribute error.


Solution

  • I think the problem is here:

    if user is None:  # <--
       login(request, user)
    

    It should be:

    if user is not None:
       login(request, user)