I want to write template that render somethings only one time.
My ideas is create flag variable for check it is first time or not ?
My code
{% with "true" as data %}
{% if data == "true" %}
//do somethings
** set data to "false" **
{% else %}
//do somethings
{% endif %}
{% endwith %}
I don't know How to change variable in django template ? (Is it possible ?)
Or
Better way for do this.
Thank you
NIKHIL RANE
This can be done with a Django custom filter
def update_variable(value):
data = value
return data
register.filter('update_variable', update_variable)
{% with "true" as data %}
{% if data == "true" %}
//do somethings
{{update_variable|value_that_you_want}}
{% else %}
//do somethings
{% endif %}
{% endwith %}
NIKHIL RANE's answer doesn't work for me. Custom simple_tag() can be used to do the job:
@register.simple_tag
def update_variable(value):
"""Allows to update existing variable in template"""
return value
and then use it like this:
{% with True as flag %}
{% if flag %}
//do somethings
{% update_variable False as flag %}
{% else %}
//do somethings
{% endif %}
{% endwith %}
来源:https://stackoverflow.com/questions/31916408/is-django-can-modify-variable-value-in-template