Baama
Baama

Reputation: 2642

extra context with django-registration

Am using django-registration but I have a problem. I want to get the login status of a user and pass it to all the html pages in my project. I normally just declare it in my views.py like this

logged_in =request.user.is_authenticated() # get login status

and then pass it as a context to corresponding htmls. now am using django-registration and I need to pass logged_in to all the htmls rendered by django-registation but am not sure how to do this. I am trying to not modify the django-registration code. I have a feeling i need context_processors but am lost as to what I really need to do here. help please !!

Upvotes: 0

Views: 410

Answers (3)

Baama
Baama

Reputation: 2642

Am using django 1.4 and apparently request is not a default context. Or as it is I could not access request by default so I added this to my settings.py and it worked for me

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.contrib.messages.context_processors.messages",
"django.core.context_processors.request",)

Of course in my case I only needed "django.core.context_processors.request" but since I am overriding the default context processors, I need to add them manually in my settings

Upvotes: 0

Poli
Poli

Reputation: 2604

By adding django.core.context_processors.request to your TEMPLATE_CONTEXT_PROCESSORS (which comes by default) you should have a request instance on the templates. You can then access the user (which may be an authenticated User or an AnonymousUser, never None) on your templates by typing:

{% if request.user.is_authenticated %}

Upvotes: 1

user1614526
user1614526

Reputation: 474

the request object itself is accessible on the html pages

better to use that in your base.html file to access it on all pages like

  {% if request.user.is_authenticated %}   

Upvotes: 0

Related Questions