python - How to choose or assign variable in django template? -


i have template:

{% if c == 2 %}     {% time in %}         code(1)     {% endfor %} {% else %}     {% time in b %}         repeat of code(1)     {% endfor %} {% endif %} 

as can see code has repeating part. want refactor this:

{% if c == 2 %}     var = {% else %}     var = b {% endif %} {% time in var %}     code(1) {% endfor %} 

how this?

don't in template(and don't think can), in views.py instead:

var = c if c == 2 else b # add template context context['var'] = var 

if add logic in template, people have @ both places figure out what's going on. if have logics in views.py it's clearer.


Comments