I have seen enough number of examples that allow me to declare a new variable inside a template and set its value. But what I want to do is to update the value of a particular variable inside the template.
For example I have a datetime field for an object and I want to add timezone according to the request.user
from the template. So I will create a template filter
like {% add_timezone object.created %}
and what it will do is that it will add timezone to object.created
and after that whenever I access {{object.created}}
it will give me the updated value.
Can anybody tell me how this can be done. I know I need to update the context
variable from the template filter. But don't know how.
You cannot modify a value in a template, but you can define 'scope' variables using the {% with %}
tag:
{% with created=object.created|add_timezone %}
Date created with fixed timezone: {{ created }}
{% endwith %}
where add_timezone
is a simple filter:
def add_timezone(value):
adjusted_tz = ...
return adjusted_tz
register.filter('add_timezone', add_timezone)