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

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

我正在尝试登录我的用户,我不断收到的错误是 AttributeError:AnonymousUser' 对象没有属性 '_meta'。

我正在关注 Django 中关于如何让用户登录的文档,我已经尝试过了,但我一直收到错误。

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)

我想要的是用户登录,但我一直收到属性错误。

我认为问题出在这里:

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

应该是:

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