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

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

问题描述

如果您在以前问过这个问题时尝试帮助我,那对不起。因为某些原因我没有被允许编辑其他信息,所以必须删除该问题。



我正在努力在我的django网站上实现用户身份验证。一切都奏效我的意见,模型,网址等都是设置的。用户可以注册,登录,注销。我遇到的问题是这一段代码:

  {%if request.user.is_authenticated%} 
< li>< a href =/ logout>注销< / a>< / li>
{%else%}
< li>< a href =/ login>登录< / a>< / li>
{%endif%}

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



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

  def LoginRequest(request):
如果request.user.is_authenticated():
返回HttpResponseRedirect('/ profile /')
如果request.method =='POST'
form = LoginForm(request.POST)
如果form.is_valid():
username = form.cleaned_data ['username']
password = form.cleaned_data ['password' ]
profile = authenticate(username = username,password = password)
如果配置文件不是无:
登录(请求,配置文件)
返回HttpResponseRedirect('/ profile /')
else:
返回render_to_response('templa te / login.html',{'form':form},context_instance = RequestContext(request))
else:
返回render_to_response('template / login.html',{'form':form}
else:
'''用户没有提交表单,显示登录表单'''
form = LoginForm()
context = {'form':form}
return render_to_response('template / login.html',context,context_instance = RequestContext(request))


解决方案

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

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

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天全站免登陆