gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:36:05 24 4
gpt4 key购买 nike

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

这是我目前所拥有的:

表格

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)

模板

{% 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 来完成,而不是对任何表单元素进行硬编码。这可能吗?

最佳答案

参见(重复)问题: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

需要时在您的 View 中调用 form.hide_value_currency(pre_set_value)

模板中不需要任何逻辑。

关于python - 在 SelectField 和 HiddenField 之间动态更改 WTForms 字段类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32407564/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com