Django'AnonymousUser'对象没有属性'_meta'登录功能无法正常工作

问题描述

{% extends 'todo/base.html' %}
    {% block content %}
    
    <h1>Log In</h1>
    
    <h4>{{error}}</h4>
    
    <form method="POST">
        {% csrf_token %} 
        {{form.as_p}}
        <button type="submit">Log In</button>
    </form>
    
    {% endblock %}

那是模板

def log_in_user(request):
    if request.method == 'GET':
        return render(request,'todo/logInUser.html',{'form': AuthenticationForm()})
    else:
        user = authenticate(request,username=request.POST['username'],password=request.POST['password'])
        if user is None:
            return render(request,{'form': AuthenticationForm(),"error": "Incorrect username or password. Try again."})
        else:
            login(request,user)
            return redirect('currentTodos')

   

这是我的功能,应该登录用户并将其重定向到成功页面。但是,它仅适用于超级用户。如果我尝试以没有用户权限的用户身份登录,则会收到并显示错误消息,我创建了return render(request,"error": "Incorrect username or password. Try again."})

我已经尝试过这样写,但是它并没有改变任何东西:

from django.contrib.auth import authenticate
user = authenticate(username='john',password='secret')
if user is not None:
# A backend authenticated the credentials
else:
# No backend authenticated the credentials

我还尝试将backed = ...添加登录功能。 无效,请帮忙。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)