On Jun 1, 7:53 pm, "bax...@gretschpages.com" <mail.bax...@gmail.com>
wrote:
> 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?

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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to