request.session in template tag?

2,813 views
Skip to first unread message

mail....@gmail.com

unread,
Jun 1, 2009, 1:53:37 PM6/1/09
to Django users
I'm trying to write a template tag that accesses the user's
request.session. My problem is, I don't know how to get the request.

Googling around, I saw references to including
TEMPLATE_CONTEXT_PROCESSORS in settings, but I'm still not seeing the
request.

Help?

K.Berkhout

unread,
Jun 1, 2009, 2:27:06 PM6/1/09
to Django users
Looks like something similar to the problem I had, see
http://groups.google.nl/group/django-users/browse_thread/thread/cf3e243d3bc5ca3b?hl=nl


Kevin

On 1 jun, 19:53, "bax...@gretschpages.com" <mail.bax...@gmail.com>
wrote:

Gustavo Henrique

unread,
Jun 1, 2009, 2:25:55 PM6/1/09
to django...@googlegroups.com
Using direct_to_template the request var is passed as param.
So, you can access in template: {{ request.session.myvar }}

--
Gustavo Henrique
Site: http://www.gustavohenrique.net
Blog: http://blog.gustavohenrique.net

Baxter

unread,
Jun 1, 2009, 2:39:02 PM6/1/09
to Django users
On Jun 1, 1:25 pm, Gustavo Henrique <gustavo...@gmail.com> wrote:
> Using direct_to_template the request var is passed as param.
> So, you can access in template: {{ request.session.myvar }}

Thanks, but what I'm really after is a template tag I can drop in
anywhere, whether the main template is generic or not. Actually, if it
were only generic templates, this would be done already.

Masklinn

unread,
Jun 1, 2009, 3:05:46 PM6/1/09
to django...@googlegroups.com

Django doesn't have "generic templates" (only generic views), and if
you don't provide the request to the template engine, it'll have a
pretty hard time getting the session without using magic.

Magic is bad.

If you want the session in your template context, either use generic
views, use RequestContext (with the Request context_processor enabled)
in your views or create your own context.

Baxter

unread,
Jun 1, 2009, 4:36:01 PM6/1/09
to Django users
> Django doesn't have "generic templates" (only generic views), and if  
> you don't provide the request to the template engine, it'll have a  
> pretty hard time getting the session without using magic.
>
> Magic is bad.
>
> If you want the session in your template context, either use generic  
> views, use RequestContext (with the Request context_processor enabled)  
> in your views or create your own context.

My apologies for saying generic templates rather than generic views.
Nevertheless, what I'm after is a template tag that's aware of the
request that I can put in any template, whether the template uses a
generic view or not. I really don't want to go through all my views
adding requestContext for this small thing. I just want a template tag
that's aware of the request.

Masklinn

unread,
Jun 1, 2009, 5:26:18 PM6/1/09
to django...@googlegroups.com
On 1 Jun 2009, at 22:36 , Baxter wrote:
> Nevertheless, what I'm after is a template tag that's aware of the
> request that I can put in any template, whether the template uses a
> generic view or not.

And I'm pretty sure you won't find it. Templatetags can't be magically
aware of "the request".

BluMarble

unread,
Jun 2, 2009, 8:55:04 AM6/2/09
to Django users


On Jun 1, 7:53 pm, "bax...@gretschpages.com" <mail.bax...@gmail.com>
wrote:
Hi your best bet is to do the following - I used this for something
else but you'll get the idea:

from django.template import resolve_variable
from django import template
from django.template import Library, Node

register = template.Library()

class AlertNode(Node):
def __init__(self, request):
self.request = request

def render(self, context):
request = resolve_variable(self.request, context)

# Do something with the session
var = request.session.get('js_alert', None)
if var:
del request.session['js_alert']
return str('<script type="text/javascript">alert("%s");</
script>' % var)
else:
return ''

@register.tag(name="get_js_alert")
def get_js_alert(parser, token):
try:
tag_name, request = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError, "%r tag requires exactly
one argument" % token.contents[0]

return AlertNode(request)

# Usage: {% load js_tag %}{% get_js_alert request %}

You now have acces to POST, GET and SESSION ect in your templatetag.

BluMarble

unread,
Jun 2, 2009, 9:06:46 AM6/2/09
to Django users
If you just need the request obj passed to all pages. I use this in my
URLs:

# Custom Direct to template - carrys the request object
def direct_to_template(request, template):
from django.shortcuts import render_to_response
from django.template import RequestContext
return render_to_response(template, {'request': request},
context_instance = RequestContext(request))

urlpatterns = patterns('',
(r'^$', 'urls.direct_to_template', {'template': 'index.html'}),
)

BluMarble

unread,
Jun 8, 2009, 5:37:39 AM6/8/09
to Django users
Thanks to a comment on my blog: the below is null and void: the
request object can be retrieved directly from context: context
['request'].
Reply all
Reply to author
Forward
0 new messages