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

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

问题描述

如果你试图帮助我,当我问这个早些时候对不起。不得不删除这个问题,因为我没有被允许编辑出于某种原因的更多信息。

我的工作我的Django的网站上实施用户身份验证。一切正常。我的看法,型号,网址等,都成立。用户可以注册,登录,注销。我遇到的问题是与该位code的:

  {%如果request.user.is_authenticated%}
      <立GT;< A HREF =/注销>注销< / A>< /李>
      {%其他%}
      <立GT;< A HREF =/登录>登录上述< / A>< /李>
      {% 万一 %}

即使当我登录,但仍显示登录作为一个选项,而不是注销。但是,如果我点击链接,它会重定向我/ profile文件,因为这是该视图告诉它做,如果我参与的记录。因此,很明显它知道我登录,但模板不的readInt user.is_authenticated是真实的。

有关登录请求的观点是:

 高清LoginRequest(要求):
    如果request.user.is_authenticated():
        返回的Htt presponseRedirect('/型材/')
    如果request.method =='POST':
        形式= LoginForm的(request.POST)
        如果form.is_valid():
            用户名= form.cleaned_data [用户名]
            密码= form.cleaned_data ['密码']
            简介=身份验证(用户名=用户名,密码=密码)
            如果配置文件不可无:
                登录(要求,配置文件)
                返回的Htt presponseRedirect('/型材/')
            其他:
                返回render_to_response('模板/ login.html的',{形式:形式},context_instance = RequestContext的(要求))
        其他:
            返回render_to_response('模板/ login.html的',{形式:形式},context_instance = RequestContext的(要求))
    其他:
        '''用户没有提交表单,让他们登录表单'''
        形式= LoginForm的()
        上下文= {形式:形式}
        返回render_to_response('模板/ login.html的'背景下,context_instance = RequestContext的(要求))


解决方案

如果在auth上下文处理器被启用,用户已经在模板的背景下,你可以这样做:

  {%如果user.is_authenticated%}

如果您要访问在模板中要求,请确保您已启用<一个href=\"https://docs.djangoproject.com/en/dev/ref/templates/api/#django-core-context-processors-request\">request上下文处理器。

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.

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 %}

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

解决方案

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

{% if user.is_authenticated %}

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

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

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