我是新来的Django,我正在创建一个简单的登录页面,并希望在用户单击登录表单中的登录按钮后立即将用户信息重定向到主页,用户应使用用户名
重定向到主页。def login1(req):
if req.method == 'POST':
user = ppl.objects.filter(username = req.POST['text'])
print(req.POST['text'])
pwd = ppl.objects.filter(pwd = req.POST['pass'])
print(req.POST['pass'])
if user and pwd:
return HttpResponseRedirect(reverse('home', {'u':user}))
else:
return render(req,'login.html',{'error':"username and password does not match"})
答案 0 :(得分:0)
def login(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = auth.authenticate(username=username, password=password)
if user is not None:
auth.login(request, user)
return redirect('home')
else:
return redirect('login')
else:
return render(request, 'login.html')
在主页模板或任何其他模板中,您可以调用{{ request.user.username }}
来查看当前登录的用户