django - 登录后显示 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。我不知道我做错了什么。
解决方案
我认为这是因为您发送的是用户对象而不是名称
req.user.username
然后你有一个错字
if req.method == 'POST':
推荐阅读
- sql-server - SQL Server 中年度数据的平均值
- php - 对数组整数值求和并合并重复的非整数值
- ruby-on-rails - 使用 Papertrail 的 attribute_name_was
- python - 在 Django 中将 JWT 与自定义用户模型一起使用时遇到问题
- javascript - 如何使用 JQuery 从 RSS 提要中提取图像?
- visual-studio - 在 IISExpress 上启动 localhost:远程设备或资源不接受连接
- android - 片段的新活动?
- node.js - 我想通过单击按钮向订阅我的网页的用户发送电子邮件
- php - PHP Composer Autoload 找不到类错误
- powerbi - 如何将来自两个查询的汇总数据加入 DAX Power BI 中的新表中