如何在 Jinja2 模板中迭代此属性



在视图方法中,我这样做:

 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%}

编辑:在视图方法中,form['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> 

您可以使用项目访问权限:

{% 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]

在视图方法中,form['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>

最新更新