在“next.html”页面登录时,如何向任何用户显示他/她的用户名???
def signin(request):
if request.method != 'POST':
return render(request, 'login.html', {'error': 'Email or Password is incorrect'})
user_profile = User_account.objects.filter(e_mail = request.POST['user_email'], passwd = request.POST['password'])
if user_profile:
return render(request, 'next.html', {'name' :User_account.objects.**WHAT SHOULD I WRITE HERE???** } )
else:
return render(request, 'login.html', {'error': 'Email or Password is incorrect'})
最佳答案
有两种方法可以做到:
a) 直接在模板中:
{{request.user.username}}
或者:
b)
return render(request, 'next.html', {'name' : request.user.username })
请注意,您还没有“登录”用户
在你的情况下,你可以这样做:
return render(request, 'next.html', {'name' : user_account.first_name })
您需要调用
login
方法来实际登录用户,或者手动处理登录。 This post 可以为您提供方法。你还需要一个 request_context processor
关于Django : show the username to the user when logged in,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17010104/