[英]Django , how to remember that a user has logged in?
@csrf_protect
def loginn(request):
c = {}
c.update(csrf(request))
return render_to_response("login/login.html",c)
@csrf_protect
def auth_view(request):
username = request.POST.get('username', '')
password = request.POST.get('password', '')
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return render_to_response('login/loggedin.html',RequestContext(request))
else:
return HttpResponseRedirect('/posts/invalid')
def loggedin(request):
return render_to_response('login/loggedin.html',
{'full_name' : request.user.username})
def invalid_login(request):
return render_to_response('login/invalid_login.html')
def logout(request):
logout(request)
return render_to_response('login/logout.html')
Here is what I have in my views.py . 这是我的views.py中的内容。 It's working , checking password , user , etc . 它正在工作,正在检查密码,用户等。 But I don't know how to remember that a user has logged in. 但是我不知道该如何记住用户已登录。
I want to show logged/not logged users different Menus. 我想向已登录/未登录的用户显示不同的菜单。 Tried it like that in my templates
: 在我的templates
尝试过:
{% block Menu %}
{% if not user.is_authenticated %}
<li> <a href="/posts/login">Login</a> </li>
<li> <a href="/posts/register/">Register</a></li>
{% endif %}
{% if user.is_authenticated %}
<li><a href="/posts">Main</a></li>
<li><a href="#">My profile</a></li>
<li><a href="/posts/logout">logout</a></li>
{% endif %}
{% endblock %}
But it works only once and only with render_to_response
+ RequestContent
All other times I have first option. 但是它只能使用一次,并且只能用于render_to_response
+ RequestContent
。 (Login , Register) (登录,注册)
So how can I remember that a user has logged in? 那么,我如何记得用户已登录?
ps sorry for my bad english PS对不起我的英语不好
Adding to Daniel Roseman's comment... 添加到Daniel Roseman的评论中...
If you want to use render_to_response and have access to the current request objects in your templates, you'll need to pass along RequestContext as well. 如果要使用render_to_response并有权访问模板中的当前请求对象,则还需要传递RequestContext。 https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#django.shortcuts.render_to_response https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#django.shortcuts.render_to_response
return render_to_response('my_template.html',
my_data_dictionary,
context_instance=RequestContext(request))
You may want to take a look at using render instead though as it already includes RequestContext for you. 您可能想看看使用render作为替代,因为它已经为您提供了RequestContext。 https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.