django - Read request variable in template tag -
i in process of turning django project multi-language site. purpose trying adopt countries-for-django
(github) package.
in 1 of templatetags, code trying read session variable django_country
(taken here), django 1.5 trips on reading request
variable context.
exception type: attributeerror exception value: 'nonetype' object has no attribute 'session'
the code in template tag stated below (the code has been extended since first post):
class getcurrentcountrynode(node): def __init__(self, variable): self.variable = variable def get_current_country(self, context): django.template import context return context.get('request').session.get('django_country') def render(self, context): context[self.variable] = self.get_current_country(context) return '' ... @register.tag("get_current_country") def do_get_current_country(parser, token): args = token.contents.split() if len(args) != 3 or args[1] != 'as': raise templatesyntaxerror("'get_current_country' requires 'as variable' (got %r)" % args) return getcurrentcountrynode(args[2])
when print context
variable, print out not contain request
variable. however, can see via django toolbar
variable existing.
did way of reading context variable change django 1.5? not find in documentation.
views.py , template added completeness.
views.py
... class startview(formview): form_class = startform template_name = 'home.html' def form_valid(self, form): self.request.session['address'] = form.cleaned_data['address'] return httpresponseredirect(reverse_lazy('new')) ...
home.html
{% load countries %} {% get_current_country country %} {% get_available_countries countries %} <head> ...
the code works if django.core.context_processors.request
added template_context_processors
in settings.py
.
Comments
Post a Comment