加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Django user.is_authenticated在视图中工作,但不在模板中工作

发布时间:2020-12-20 11:26:50 所属栏目:Python 来源:网络整理
导读:我有一个视图,并登录用户: def login_user(request):c = {}c.update(csrf(request))username = password = ''if request.POST: username = request.POST.get('username') password = request.POST.get('password') user = authenticate(username=username,p
我有一个视图,并登录用户:

def login_user(request):
c = {}
c.update(csrf(request))

username = password = ''
if request.POST:
    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)
            c.update(user(request))
            return redirect('/module/')
        else:
            return redirect('/inactive/')
    else:
        return redirect('/failure/')

return render_to_response('core/auth.html',c)

这正确登录用户,因为我可以访问django管理页面,如果我在我的超级用户.

登录并重定向后,我想在屏幕上显示用户名,目前我正在使用

{% if user.is_authenticated %}
            <p class="navbar-text pull-right">Welcome,{{ user.username }}. Thanks for logging in.</p>
          {% else %}
            <p class="navbar-text pull-right">Welcome,new user. Please log in.</p>
          {% endif %}

但它似乎总是相信用户没有登录.任何帮助将不胜感激.

编辑:这是我的模板上下文处理器

TEMPLATE_CONTEXT_PROCESSORS = (
 "django.contrib.auth.context_processors.auth","django.core.context_processors.debug","django.core.context_processors.i18n","django.core.context_processors.media","django.core.context_processors.static","django.core.context_processors.tz","django.contrib.messages.context_processors.messages","django.core.context_processors.request",)

解决方法

您是否尝试过向模板发送RequestContext对象?

return render_to_response('core/auth.html',c,context_instance=RequestContext(request))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读