如何将变量从函数传递到WTForm?我使用变量选择RadioField选项

时间:2019-04-19 02:59:41

标签: python-2.7 flask-wtforms

我正在尝试将会话变量传递给WTForm。我使用变量选择RadioField条目。

我试图将其作为常规变量传递,但是WTForm无法识别该变量。我尝试了不同的参数,但在WTForms中仍然无法识别。这是我传递的可变级别。我知道,如果我在类ZWOForm(Form)的第一行上写level = 1(或2或3),那么一切都将起作用:

来自 init.py

class ZWOForm(Form):
    zwotitle = TextField('Title', [validators.Length(min=3, max=50)])
    zwotags = TextField('Tags', [validators.Length(min=3, max=50)])
    category = TextField('Category (Your Choice)', [validators.Length(min=3, max=50)])
    if level <3:
        if level <2:
            zwolevel = RadioField('Viewing Level', choices=[('1','Public')], default=1)
        else:
            zwolevel = RadioField('Viewing Level', choices=[('1','Public'),('2','Private')], default=1)
    else:
        zwolevel = RadioField('Viewing Level', choices=[('1','Public'),('2','Private'),('3','Team')], default=1)
        zwodescription = TextAreaField('Description', [validators.Length(min=3, max=200)])      


@app.route('/createzwo/', methods=["GET","POST"])
@login_required
def create_zwo():
    try:
        level = session['level']
        form = ZWOForm(request.form)
        if request.method == "POST" and form.validate():
...
        return render_template("createzwo.html", form=form)

从HTML表单:

{% from "_formhelpers.html" import render_field %}
<form method=post action="/createzwo/">
    <dl>
        {{render_field(form.zwotitle)}}
        {{render_field(form.zwotags)}}
        {{render_field(form.category)}}
        {{render_field(form.zwolevel)}}
        {{render_field(form.zwodescription)}}
    </dl>
    <p><input type=submit value="Create ZWO File"></p>
</form>

来自_formhelpers.html:

{% macro render_field(field) %}
    <dt>{{ field.label }}
    <dd>{{ field(**kwargs)|safe }}
        {% if field.errors %}
            <ul class=errors>
            {% for error in field.errors %}
                <li>{{ error }}</li>
            {% endfor %}
            </ul>
        {% endif %}
    </dd>
{% endmacro %}

我所看到的错误是“级别”未定义,并且渲染错误使我们吐出一堆指向其他.py文件(本地和全局)的行。

1 个答案:

答案 0 :(得分:0)

表单的类定义中的代码仅在模块最初被加载时执行一次,并且此时本地变量level不存在。即使当时确实存在,您也只能设置一次类定义,而您希望每次显示时都可以更改表单选择。这可以在您的视图功能中轻松实现。不要在表单类中设置字段选择,而是在创建表单后通过设置字段选择属性的值在表单本身上进行设置。假设每个级别可用的选择数量恰好等于该级别的值,则可以使用列表切片来生成此列表。

class ZWOForm(Form):
    zwotitle = TextField('Title', [validators.Length(min=3, max=50)])
    zwotags = TextField('Tags', [validators.Length(min=3, max=50)])
    category = TextField('Category (Your Choice)', [validators.Length(min=3, max=50)])
    zwolevel = RadioField('Viewing Level', choices=[], default=1)
    zwodescription = TextAreaField('Description', [validators.Length(min=3, max=200)])      

@app.route('/createzwo/', methods=["GET","POST"])
@login_required
def create_zwo():        
    try:
        form = ZWOForm(request.form)
        all_choices = [('1','Public'),('2','Private'),('3','Team')]
        choices = all_choices[:session["level"]]
        form.zwolevel.choices = choices

        if request.method == "POST" and form.validate():
            ...
        return render_template("createzwo.html", form=form)