在 Django 模板中遇到 user.is_authenticated 问题 [英] Having trouble with user.is_authenticated in django template

查看:18
本文介绍了在 Django 模板中遇到 user.is_authenticated 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果您在我之前提出这个问题时试图帮助我.由于某种原因,我无法编辑其他信息,因此不得不删除该问题.

Sorry if you tried helping me when I asked this earlier. Had to delete that question because I wasn't being allowed to edit additional information for some reason.

我正在我的 django 网站上实施用户身份验证.一切正常.我的views、models、urls等都设置好了.用户可以注册、登录、注销.我遇到的问题是这段代码:

I'm working on implementing user authentication on my django website. Everything works. My views, models, urls, etc. are all set up. Users can register, log in, log out. The issue I'm having is with this bit of code:

{% if request.user.is_authenticated %}
      <li><a href="/logout">Log Out</a></li>
      {% else %}
      <li><a href="/login">Log In</a></li>
      {% endif %}

即使我已登录,它仍然显示登录"作为选项,而不是注销".但是,如果我点击链接,它会将我重定向到/profile 因为如果我登录,视图会告诉它这样做.所以,很明显它知道我已登录,但模板不是readint user.is_authenticated 为真.

Even when I'm logged in, it is still displaying "Log In" as an option rather than "Log Out". However, if I click on the link, it'll redirect me to /profile because that's what the view tells it to do if I'm logged in. So, clearly it knows I'm logged in, but the template isn't readint user.is_authenticated as true.

与登录请求相关的视图是:

The view relating to login requests is:

def LoginRequest(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/profile/')
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            profile = authenticate(username=username, password=password)
            if profile is not None:
                login(request, profile)
                return HttpResponseRedirect('/profile/')
            else:
                return render_to_response('template/login.html', {'form': form}, context_instance=RequestContext(request))
        else:
            return render_to_response('template/login.html', {'form': form}, context_instance=RequestContext(request))
    else:
        ''' user is not submitting the form, show them login form ''' 
        form = LoginForm()
        context = {'form': form}
        return render_to_response('template/login.html', context, context_instance = RequestContext(request))

推荐答案

如果启用了 auth 上下文处理器,则 user 已经在模板上下文中,您可以这样做:

If the auth context processor is enabled, then user is already in the template context, and you can do:

{% if user.is_authenticated %}

如果您想访问模板中的request,请确保您已启用请求上下文处理器.

If you want to access request in the template, make sure you have enabled the request context processor.

在您的问题中,您使用的是 render_to_response.从 Django 1.3 开始,最好使用 render 而不是 render_to_response.将 render_to_responseRequestContext(request) 一起使用在 Django <= 1.9 中有效,但从 Django 1.10 开始,如果需要,您必须使用 render 快捷方式上下文处理器工作.

In your question you are using render_to_response. Ever since Django 1.3, it has been better to use render instead of render_to_response. Using render_to_response with RequestContext(request) works in Django <= 1.9, but from Django 1.10 onwards you must use the render shortcut if you want the context processors to work.

return render(request, 'template/login.html', context)

这篇关于在 Django 模板中遇到 user.is_authenticated 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆