0

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 !!

flexxxit
  • 2,440
  • 5
  • 42
  • 69
  • The request object is accessible in the html files, so {% if request.user.is_authenticated %} ... {% endif %} would work – karthikr Aug 29 '12 at 13:44
  • @karthikr Yes but the problem is that request is not part of the contexts in django-registration. how do I add this extra context ? help – flexxxit Aug 29 '12 at 13:54
  • Well, the request_context would have the request object. It does not matter whether which app you use. In the HTML, you do not have to do anything different to access the request object. A plain {{request}} would give you access to it. – karthikr Aug 29 '12 at 14:07
  • @karthikr It seems you don't get me. django registration form returns a dictionary like this `{'form':form}`, request is not passed to the template so when i try it, it does not exist in my base template. ` {% if request.user.is_authenticated %}` always gives a false value. i was thinking of adding request as an extra context or? – flexxxit Aug 29 '12 at 14:22
  • My guess is, you have a bigger problem than just sending a parameter here. The reason is, You are trying to register, and the 3rd party app may not have addressed the scenario for logged in users. Or, you may be getting confused between registration and authentication. Try logging in, and then {% if request.user.is_authenticated %} - You should not be sending in any additional paramters to access the request object – karthikr Aug 29 '12 at 14:30
  • @YehonathanQuartey, do you have any `TEMPLATE_CONTEXT_PROCESSORS` in your `setting.py` file? – Poli Aug 29 '12 at 17:35

3 Answers3

1

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 %}
Poli
  • 2,514
  • 2
  • 18
  • 12
0

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 %}   
user1614526
  • 474
  • 1
  • 4
  • 13
  • Thanks but the thing is that django registration does not include the `request` as part of the context. so how do I add it as an extra context ? – flexxxit Aug 29 '12 at 14:02
  • @YehonathanQuartey karthikkr is right context has request object try it first it will work – user1614526 Aug 29 '12 at 14:12
0

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

flexxxit
  • 2,440
  • 5
  • 42
  • 69