In Django, is it possible to access the current user session from a user tag?

I am writing a custom tag in Django that should output the value stored in a user session, but I cannot find a way to access the session object from the user tag function. Is there a way to do this without having to manually assign a session object to a context variable?

+23
django django-templates
Dec 02 '08 at 20:01
source share
5 answers

You should be able to add a request context handler to the settings.py file:

TEMPLATE_CONTEXT_PROCESSORS = ("django.core.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n", "django.core.context_processors.media", 'django.core.context_processors.request',) 

This will do the same as the current answer, without having to add a custom file.

+23
Dec 02 '08 at 23:33
source share

No crime was intended for Sebastian, as it seemed like a useful hack at some point, but, oddly enough, on December 24th on a blog about accessing user data in Admin, James Bennett, Django release manager, said: a href = "http://www.b-list.org/weblog/2008/dec/24/admin/" rel = "noreferrer"> about using threadlocal hack:

Big rejection of fat: for these types of functions there are many, many potential possibilities. Many of them are wrong and stupid, and you should not try them .... In addition, you will sometimes see that someone suggests that these functions can be obtained using the "threadlocal hack"; this is mainly due to the inclusion of request.user in a kind of magical globally accessible variable and a very bad thing to use if you don't know what you are doing. Its also, as a rule, a very bad thing to use, even if you know what you are doing, because you probably just do it because you are lazy and do not want you to exchange information correctly. Therefore, if you see someone suggesting you do this using threadlocal, ignore that person.

Not to mention that you should ignore Sebastian, but it might be worth using other features rather than using threadlocal, which is not considered best practice.

+6
Jan 08 '09 at 3:04
source share

You can do this with custom context processors (see http://docs.djangoproject.com/en/dev/ref/templates/api/ )

In this case, you must create a new file called context_processors.py at the same level as the settings.py file containing:

 def add_session(request): return {'session': request.session} 

Then in the settings.py file add:

 TEMPLATE_CONTEXT_PROCESSORS = ("django.core.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n", "django.core.context_processors.media", 'context_processors.add_session',) 

Now you can refer to the ['session'] context in your custom tag.

Please note that this will only work for templates created using the assigned RequestContext, as in the following code:

 def test(request): return render_to_response('test.html',{}, context_instance=RequestContext(request)) 
+4
Dec 02 '08 at 20:33
source share

I found this useful: http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser

You can use middleware to retrieve user information and save it using a local stream, and then use it with the tag definition.

0
Jan 01 '09 at 21:06
source share

Using render_to decorator from django-annoying seems to be the best option (as can be seen from https://stackoverflow.com/a/90906/... )

0
Jul 25 '09 at 12:19
source share



All Articles