首页 > 解决方案 > 登录后显示 AnonymousUser 的 django 登录

问题描述

def loggin(req):
    if req.user.is_authenticated:
        return redirect('home')
    if req.POST:
        username=req.POST['username']
        password=req.POST['password']
        usr = authenticate(req,username=username,password=password)
        if usr is not None:
            login(req,usr)
            return redirect(reverse('home'))
    return render(req,'cart/login.html')
def home(req):
    if req.user.is_authenticated:
        name=req.user
        context = {
                'name':name,
        }
        return render(req,'cart/home.html',context)

我的 settings.py 很好,代码没有错误,但成功登录后重定向到主视图显示 AnonymousUser。我不知道我做错了什么。

标签: django

解决方案


我认为这是因为您发送的是用户对象而不是名称

req.user.username

然后你有一个错字

if req.method == 'POST':

推荐阅读