gpt4 book ai didi

javascript - 将 Select2 与 flask-wtforms 一起使用

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

在 select2 操作下拉字段后,form.owner_id.data 的常规使用会产生 None。如何从 select2 字段中提取选定的选项。

如果我禁用 select2 javascript,wtforms 将正常工作,我将能够使用 form.owner_id.data。 (但看起来很难看)

screenshot of rendered form

表单.py

class ProjectForm(FlaskForm):
owner_id = SelectField('Owner:', [validators.Required()], choices=[], render_kw={"placeholder": "Owner company *"})

(为简单起见,其余表格已被 chop )

View .py

@app.route('/new_project/<int:company_id>', methods=('GET', 'POST'))
def new_project(company_id):
membership = Membership.query.filter_by(user_id=current_user.id, company_id=company_id).first()
company = Company.query.filter_by(id=company_id).first_or_404()
form = ProjectForm()

form.owner_id.choices = [(str(comp.id), repr(comp)) for comp in Company.query.all()]

form.client_id.choices = [(str(comp.id), repr(comp)) for comp in Company.query.all()]

form.contractor_id.choices = [(str(comp.id), repr(comp)) for comp in Company.query.all()]

form.membership_id.choices = [(str(comp.id), repr(comp)) for comp in Company.query.all()]

if request.method == 'POST':
flash(str(form.owner_id.data), 'success')

if form.validate_on_submit():
project = Project()
connection = Assignment()

# PROJECT DETAILS
project.title = form.title.data
project.description = form.description.data
project.owner_id = int(form.owner_id.data)

_宏

{% macro render_select_field(field, placeholder='Select...', label='Select an option below') %}
<div class="form-group">
<label>{{ label }}</label>
<select data-placeholder="{{ placeholder }}" class="select-size-xs">
<option></option>
{% for choice in field.choices %}
<option value="{{ choice[0] }}">{{ choice[1] }}</option>
{% endfor %}
</select>
{% if field.errors %}
{% for error in field.errors %}
<span class="help-block text-danger"><i class="icon-cancel-circle2 position-left"></i>{{ error }}</span>
{% endfor %}
{% endif %}
</div>
{% endmacro %}

html

<form method="POST" action="{{ url_for('new_project', company_id=company.id) }}" enctype="multipart/form-data" role="form">
<div class="panel panel-body login-form">
<div class="text-center">
<div class="icon-object text-muted"><i class="icon-plus2"></i></div>
</div>

{{ form.hidden_tag() }}
<div class="text-center form-group"><span>Project details</span></div>
{{ new_render_field(form.title, icon="icon-quill2", class_='form-control') }}

{{ new_render_field(form.description, icon="icon-stack-text", class_='form-control', rows=10) }}

{{ render_select_field(form.owner_id, placeholder="Who's doing the work?", label="Select the owner company") }}

{{ render_select_field(form.client_id, placeholder="Where do the bills go?", label="Select the client company") }}

{{ new_render_field(form.default_client_rate, icon="icon-price-tag", class_='form-control') }}

{{ new_render_field(form.default_contractor_rate, icon="icon-price-tag", class_='form-control') }}


<!-- THE REST OF FORM HAS BEEN TRUNCATED FOR SIMPLICITY -->
<div class="form-group">
<button type="submit" class="btn bg-pink-400 btn-block">Create</button>
</div>
</div>
</form>

最佳答案

因此在检查窗口中分析选择字段后,很明显选择字段缺少 wtforms 需要的 name="{{ field.name }}" 以验证形式。在我的 _macro 中所做的简单更改来自于此:

<select data-placeholder="{{ placeholder }}" class="select-size-xs">

为此:

<select data-placeholder="{{ placeholder }}" class="select-size-xs" name="{{ field.name }}">

添加后,wtforms 现在可以验证并找到返回 form.owner_id.data 正确 ID 的选定选项。

关于javascript - 将 Select2 与 flask-wtforms 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51503242/

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