首页 > 代码库 > flask jinja的宏

flask jinja的宏

form中关于表单的定义

class AreaListForm(Form):
    area1 = BooleanField(u1区, default=False)
    area2 = BooleanField(u2区, default=False)
    area3 = BooleanField(u3区, default=False)
    area4 = BooleanField(u4区, default=False)
    area5 = BooleanField(u5区, default=False)
    area6 = BooleanField(u6区, default=False)
    area7 = BooleanField(u7区, default=False)
    area8 = BooleanField(u8区, default=False)
    area9 = BooleanField(u9区, default=False)

class Good_retainForm(AreaListForm):
    user_account = StringField(u用户账号, validators=[DataRequired()])
    submit = SubmitField(u查询)

 

视图函数中的定义

@main.route(/goods_retain, methods=[GET, POST])
@login_required
def goods_retain():
    form = Good_retainForm()
    return render_template(goods_retain.html, form=form)

模板中的宏定义

{% macro area_form(form) %}
    {{ form.area1 }}
    {{ form.area1.label }}
    {{ form.area2 }}
    {{ form.area2.label }}
    {{ form.area3 }}
    {{ form.area3.label }}
    {{ form.area4 }}
    {{ form.area4.label }}
    {{ form.area5 }}
    {{ form.area5.label }}
    {{ form.area6 }}
    {{ form.area6.label }}
    {{ form.area7 }}
    {{ form.area7.label }}
    {{ form.area8 }}
    {{ form.area8.label }}
    {{ form.area9 }}
    {{ form.area9.label }}
{% endmacro %}

模板中引用宏

{% from "_area_marco.html" import area_form %}
{{ area_form(form) }}

 

flask jinja的宏