Django:模板上下文处理器请求变量 [英] Django: Template context processor request variable
问题描述
我正在尝试实施 django-facebookconnect ,因为我需要检查如果用户通过Facebook或普通用户登录。
I am trying to implement django-facebookconnect, for I need to check if a user logged in via Facebook or a regular user.
在模板中,我可以通过检查请求来检查用户是否通过Facebook登录。如果您的帐号是
,请执行以下操作:
At the template, I can check if user logged in via facebook by checking request.facebook.uid
such as:
{% if is_facebook %}
{% show_facebook_photo user %}
{% endif %}
为此,我需要要将 is_facebook':request.facebook.uid
传递给模板,我将在任何地方使用它,因此我想尝试将其应用于现有的模板上下文处理器,并调用在base.html上面的snipplet,对于Foo对象,它可以正常工作:
For this, I need to pass is_facebook': request.facebook.uid
to the template and I will be using this in everywhere thus I want tried to apply it to an existing template context processor and call the snipplet above at the base.html, and it works fine for Foo objects:
def global_variables(request):
from django.conf import settings
from myproject.myapp.models import Foo
return {'is_facebook': request.facebook.uid,'foo_list': Foo.objects.all()}
我可以在任何视图下列出Foo对象,没有任何问题,但它失败这个新的 is_facebook
,它根本不返回任何东西。
I can list Foo objects at any view without any issue however it fails for this new is_facebook
, it simply returns nothing.
如果我传递'is_facebook':request.facebook.uid每个单一的视图,它的工作,但我需要这个全球任何视图呈现。
If I pass 'is_facebook': request.facebook.uid in every single view , it works but I need this globally for any view rendering.
推荐答案
如果您可以通过请求
对象,为什么你需要添加一个特殊的 is_facebook?/ / code> boolean?只需启用内置
django.core.context_processors.request
,这将确保请求
存在于所有模板中,那么你可以这样做:
If you have access via the request
object, why do you need to add a special is_facebook
boolean at all? Just enable the built-in django.core.context_processors.request
and this will ensure that request
is present in all templates, then you can do this:
{% if request.facebook.uid %}
这篇关于Django:模板上下文处理器请求变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!