如何在jinja2模板中迭代这些属性?

时间:2014-02-15 17:03:06

标签: python jinja2

在view方法中,我这样做:

 for i in range(0,15):
       setattr(object, 'f%d' % i, 'abc')

所以现在我有属性f1,f2,f3,f4,f5。 那我怎么能在jinja2模板中迭代它?

像这样,或者我不知道:

 {%for i in range(0,15)%}
   {{f}}i
{%endfor%}

修改: 在视图方法中,表单['select%d'| format(i)]不起作用,但在模板中它确实

def method():       
class F(Form):
    pass
count=0

for attempt in e.attempts_of_exam:
    setattr(F, 'select%d'%count, SelectField(attempt.attempter.username,
                                    validators=[validators.optional()],
                                    choices=[('A', 'A'), ('B', 'B'), ('C', 'C'),
                                     ('D', 'D'), ('E', 'E'), ('FX', 'FX'),
                                     ('n/a', 'n/a')]))
    count +=1

form = F()
i =0
if form.validate_on_submit():
    for attempt in e.attempts_of_exam:
        attempt.result = form['select%d'|format(i)].data
        i +=1
    db.session.commit()
    return redirect(url_for('attempts_of_exam',id=e.id))


else:
    return render_template('update_exam_results.html',
        form=form,
        count=count,
        exam = e)

然后在模板中

{{ form.hidden_tag() }}
    <table border='1'>
            {% for i in range(count) %}
            <tr>
                <td> 
                    {{ form['select%d'|format(i)].label}}
                </td>
                <td>
                    {{ form['select%d'|format(i)]}}
                </td>
            </tr>
            {% endfor %}
    </table> 

2 个答案:

答案 0 :(得分:2)

您可以使用商品访问权限:

{% for i in range(15) %}
    {{ object['f%d'|format(i)] }}
{% endfor %}

但你最好只使用一个列表。

对于表单,只需遍历表单对象:

{% for field in form %}
    <td>
        {{ field.label }}
    </td>
    <td>
        {{ field }}
    </td>
{% endfor %}

在您的视图中,您可以访问form.data对象中的结果;这只是一本字典:

if form.validate_on_submit():
    for i, attempt in enumerate(e.attempts_of_exam):
        attempt.result = form.data['select%d' % i]

答案 1 :(得分:0)

在视图方法中,表单['select%d'| format(i)]不起作用,但在模板中它确实

def method():       
class F(Form):
    pass
count=0

for attempt in e.attempts_of_exam:
    setattr(F, 'select%d'%count, SelectField(attempt.attempter.username,
                                    validators=[validators.optional()],
                                    choices=[('A', 'A'), ('B', 'B'), ('C', 'C'),
                                     ('D', 'D'), ('E', 'E'), ('FX', 'FX'),
                                     ('n/a', 'n/a')]))
    count +=1

form = F()
i =0
if form.validate_on_submit():
    for attempt in e.attempts_of_exam:
        attempt.result = form['select%d'|format(i)].data
        i +=1
    db.session.commit()
    return redirect(url_for('attempts_of_exam',id=e.id))


else:
    return render_template('update_exam_results.html',
        form=form,
        count=count,
        exam = e)

然后在模板中

{{ form.hidden_tag() }}
    <table border='1'>
            {% for i in range(count) %}
            <tr>
                <td> 
                    {{ form['select%d'|format(i)].label}}
                </td>
                <td>
                    {{ form['select%d'|format(i)]}}
                </td>
            </tr>
            {% endfor %}
    </table>