pseudosudo
pseudosudo

Reputation: 6590

Declaring array literals in django templates

In a django template I have a <select> element with something like:

<option value="0">0</option>
<option value="10">10</option>
<option value="15">20</option>
<option value="30">30</option>
<option value="45">45</option>
<option value="60">60</option>

It's rather ugly and redundant to list out this big mess, I'd like to do something like :

{% for i in list(0, 10, 20, 30, 45, 60) %}
<option value="{{i}}">{{i}}</option>
{% for %}

I suppose I could just put these variables in a context variable, but I don't want to involve the view here, it seems like the kind of thing that should stay at the template level.

Upvotes: 1

Views: 784

Answers (1)

Timmy O&#39;Mahony
Timmy O&#39;Mahony

Reputation: 53981

This is exactly the opposite of what you should be doing in the template. Your forms should be assembled at view level and passed to the template via the context. The template should minimal logic and simple display the content from your view. If you use django's Form and ModelForm classes you can have django do all the work for you

Upvotes: 1

Related Questions