在SelectField和HiddenField之间动态更改WTForms字段类型

时间:2015-09-04 22:43:51

标签: python flask flask-wtforms

我有一个WTForms字段(value_currency),我希望有时候它是一个SelectField,有时候是一个HiddenField。我为页面使用相同的视图和模板,既可以创建新项目,也可以编辑现有项目。如果我加载页面来创建一个新项目,我希望这个字段是一个SelectField,如果我加载页面来编辑现有项目,我希望这个字段是一个HiddenField,因为它是一个不可编辑的字段。

这是我到目前为止所做的:

FORM

class PromoForm(Form):
    value = StringField('value')
    currencies = Currency.query.order_by(Currency.id).all()
    currency_choices = []
    for currency in currencies:
        currency_choice = (currency.id, currency.name)
        currency_choices.append(currency_choice)
    value_currency = SelectField('value_currency', choices=currency_choices)

查看

@app.route('/promo/<id>', methods=['GET', 'POST'])
@login_required
def promo(id):
    form = PromoForm()
    # Existing promo will pass in its id
    # id = 0 if a new promo is to be created
    if id != str(0):
        # Load existing promo
        promo = Promo.query.get(id)
        # display value in decimal format
        form.value.default = "{0}.{1:0>2}".format(
            promo.value_cents//100, promo.value_cents%100)
        form.process()
        return render_template('promo.html', promo=promo, form=form)
    else:
        # New promo
        audit_log('GET', client, session=session)
        return render_template('promo.html', form=form)

TEMPLATE

{% extends "base.html" %}
{% block content %}
    {% if promo is defined %}
        <form action="{{ url_for('.promo', id=promo.id) }}" method="post">
    {% else %}
        <form action="{{ url_for('.promo', id=0) }}" method="post">
    {% endif %}
    {{ form.hidden_tag() }}
    <div>
        <label for="value">Promo Value</label>
        {% if promo is defined %}
            {{ form.value() }}
        {% else %}
            {{ form.value() }}
        {% endif %}
        {% for error in form.value.errors %}
            <span class="error">[{{ error }}]</span>
        {% endfor %}
        {% if promo is defined %}
            # ----> Promo.value_currency should be a hidden field here (Doesn't work)
            {{ promo.value_currency }}
        {% else %}
            # ----> Promo.value_currency is a select field here (Currently works)
            {{ form.value_currency() }}
        {% endif %}
    </div>
    <div class="submit_btn">
        {% if promo is defined %}
            <input type="submit" value="Update Promo">
        {% else %}
            <input type="submit" value="Create Promo">
        {% endif %}
    </div>
{% endblock %}

我知道我只需简单地对隐藏的输入元素进行硬编码并使用Jinja输入值,但我更喜欢使用WTForms进行操作而不进行任何表单元素的硬编码。这可能吗?

1 个答案:

答案 0 :(得分:1)

查看(重复)问题:Flask, WTForms: Is there a way to make a StringField in the form _temporarily_ hidden?

您不能只是省略字段,也不能更改其对象类型(从SelectField到HiddenField)。

但是,您可以动态更改其小部件对象。 将其替换为HiddenInput

from wtforms.widgets import HiddenInput

class PromoForm(Form):
    value = StringField('value')
    currencies = Currency.query.order_by(Currency.id).all()
    currency_choices = []
    for currency in currencies:
        currency_choice = (currency.id, currency.name)
        currency_choices.append(currency_choice)
    value_currency = SelectField('value_currency', choices=currency_choices)

    def hide_value_currency(self, value):
        """
        Hide the value_currency field by morping it into a 
        HiddenInput.    
        """ 
        self.value_currency.widget = HiddenInput()
        # wtforms chokes if the data attribute is not present
        self.value_currency.data = value
        # wtforms chokes on SelectField with HiddenInput widget
        # if there is no _data() callable
        self.value_currency._value = lambda: value

在需要时在视图中致电form.hide_value_currency(pre_set_value)

模板中没有必要的逻辑。