简体   繁体   English

我无法在Django模板中吸引用户

[英]I can't get the user in django templates

I already saw all the links on stackoverflow related to this. 我已经看到了关于stackoverflow的所有链接。 I am using Django 1.9.7 an I try to see in template if theuser is authenticated but I can't get the user. 我正在使用Django 1.9.7,我尝试在模板中查看用户是否已通过身份验证,但我无法获得用户。

Template code which is not printing anything: 模板代码不打印任何内容:

{{ user.is_authenticate }}
{{ request.user.is_authenticate }}

Settings code: 设置代码:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'social.apps.django_app.context_processors.backends',
                'social.apps.django_app.context_processors.login_redirect',
            ],
        },
    },
]

I already tried withou social.apps and still not working, any ideas? 我已经尝试了social.apps,但仍然无法正常工作,有什么想法吗?

Make sure you pass the request context in the view, it's what adds the user to the context... 确保在视图中传递请求上下文,这就是将用户添加到上下文中的原因...

from django.template import RequestContext

def some_view(request):
  form = None
  # request.user accesses user here
  return render_to_response('some_template.html', {'form':form}, context_instance=RequestContext(request))

Now in the template you can use: 现在,您可以在模板中使用:

{% if request.user.is_authenticated %}{{ request.user.email }}{% endif %}

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

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