I've got a for loop in my django template that prints out a whole bunch of results (each result is t开发者_开发问答ime stamped). I want to print an arbitrary piece of data (for example a <hr />) once the timestamp reaches a certain value but I only want to print it once.
{% for result in set %}
{% if some_time > result.time %}
<hr />
{% endif %}
{{ result.info }}
{% endfor %}
This is fine except it will print the HR for every result for which the above equates to true. What I'm after is for it to only be printed the first time.
Something like:
{% if some_time > result.time and bool_flag %}
Now, since Django doesn't support assigning variables in its templates, I'm at a bit of a loss for what to do. I can think of a few ways to do it (using a template-accessible class/function that switches the flag, or alternatively doing more processing in the view, but I fear that will increase the number of database calls and iterations on the data), but I'm wondering if there is a nice simple "djangoey" way of doing this.
Thanks =)
You can use builtin ifchanged template tag:
{% ifchanged %}
{% if some_time > result.time %}
<hr />
{% endif %}
{% endifchanged %}
精彩评论