Hellnar
Hellnar

Reputation: 64793

Django: Template context processor request variable

I am trying to implement django-facebookconnect, for I need to check if a user logged in via Facebook or a regular user.

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

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

I can list Foo objects at any view without any issue however it fails for this new is_facebook, it simply returns nothing.

If I pass 'is_facebook': request.facebook.uid in every single view , it works but I need this globally for any view rendering.

Upvotes: 0

Views: 1505

Answers (2)

Bryan Matthews
Bryan Matthews

Reputation: 1136

It could be a timing issue. Make sure that the Common middleware comes before the facebook middleware in your settings file. You can probably debug and see when the facebook middleware is modifying the request and when your context processor is invoked. That may give you some clue as to why this is happening. But, as Daniel said, you can always just use the request object in your templates.

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599610

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

Upvotes: 1

Related Questions