简体   繁体   English

Django,请记住用户已登录

[英]Django , remembering that a user is logged in

So , I want to show logged/not logged users different menus. 因此,我想向已登录/未登录的用户显示不同的菜单。

If user isn't logged in: (Login , Register) 如果用户未登录:(登录,注册)

If he is already logged in: (Main , My profile , Logout) 如果他已经登录:(主要,我的个人资料,注销)

views.py: views.py:

@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')

and template: 和模板:

{% block Menu %}
{% if not request.user.is_authenticated %}
  <li>  <a href="/posts/login">Login</a> </li>
  <li>  <a href="/posts/register/">Register</a></li>
{% endif %}

{% if request.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 %}

When in my LogIn page I click on Submit , on next page it works perfectly. 在“登录”页面中,我单击“提交”,在下一页上,它可以完美运行。 There is Menu option for logged in users. 有用于登录用户的菜单选项。 But then , whatever I'll do (refresh page , go to main page ,etc) I'll see Menu option for not logged in users. 但是之后,无论我做什么(刷新页面,转到主页等),我都会看到未登录用户的菜单选项。

I've read django documentation for user authentication and now I know that {% if user.is_authenticated %} in templates works only with RequestContext and If I get it right , there is no way to do what I want this way. 我已经阅读了有关用户身份验证的django文档,现在我知道模板中的{% if user.is_authenticated %}仅适用于RequestContext ,如果我做对了,就无法用这种方式做我想要的事情。

I want Django to remember that user is logged in all time regardless of what I'm doing (refreshing page , clicking links ,etc) Is there any way to do this in Django? 我希望Django记住无论我在做什么(刷新页面,单击链接等),该用户始终处于登录状态。在Django中, 有什么方法可以做到这一点?

So how can I remember that a user has logged in and use it in template? 那么,我怎么还记得用户已经登录并在模板中使用它呢?

Maybe there is another way to remember that user is logged in and use it in template? 也许还有另一种方式可以记住用户已登录并在模板中使用它? Something with sessions, cookis , etc? 会议,炊具等问题?

ps sorry for my bad english PS对不起我的英语不好

First point: to make sure you have your context processors working, you have to use a RequestContext in all your views. 第一点:要确保上下文处理器正常工作,必须在所有视图中使用RequestContext The simplest way is to use the render() shortcut ( https://docs.djangoproject.com/en/1.6/topics/http/shortcuts/#render ) instead of render_to_response() . 最简单的方法是使用render()快捷方式( https://docs.djangoproject.com/en/1.6/topics/http/shortcuts/#render )代替render_to_response()

Second point: HTTP is a stateless protocol, so for your app to remember anything between requests, you need session support. 第二点:HTTP是无状态协议,因此,为了使您的应用记住请求之间的所有内容,您需要会话支持。 From what you describe, I strongly suspect you forgot to enable the SessionMiddleware (cf https://docs.djangoproject.com/en/1.6/topics/auth/ ). 根据您的描述,我强烈怀疑您忘记启用SessionMiddleware(cf https://docs.djangoproject.com/en/1.6/topics/auth/ )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM