gpt4 book ai didi

python - 显示 wtforms SelectMultipleField 显示为下拉列表而不是列表

转载 作者:行者123 更新时间:2023-12-05 05:50:06 25 4
gpt4 key购买 nike

我正在使用 wtforms SelectMultipleField 呈现带有选项列表的表单。

使用普通的 Vanilla SelectField 将选项呈现为下拉列表 (dropdown)。

但是,我需要能够在列表中选择多个条目,因此想使用 SelectMultipleField。然而,这显示为列表框 ( listbox )。

这是我的表单条目代码:test = SelectMultipleField('test', choices=[(1, 'a'), (2, 'b'), (3, 'c')])

渲染代码:<a class="test" id="test">{{ form.test }}</a>

如何呈现我的 SelectMultipleField 以显示为下拉列表?

最佳答案

更新:找到“ Vanilla ”解决方案!在这里:

A similar question was asked here .我采用了基础并将其调整为 flask/jinja 工作流程:

ma​​in.py:

from flask import Flask, render_template, request
from flask_wtf import FlaskForm
from wtforms.fields import SelectMultipleField, SubmitField
from wtforms import widgets

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret_key'


choices = [
'apple',
'banana',
'cherry',
]


# This code from WTForms docs, this class changes the way SelectMultipleField
# is rendered by jinja
# https://wtforms.readthedocs.io/en/3.0.x/specific_problems/
class MultiCheckboxField(SelectMultipleField):
"""
A multiple-select, except displays a list of checkboxes.

Iterating the field will produce subfields, allowing custom rendering of
the enclosed checkbox fields.
"""
widget = widgets.ListWidget(prefix_label=False)
option_widget = widgets.CheckboxInput()


class Form(FlaskForm):
global choices
select_multiple_field = MultiCheckboxField(choices=choices)
submit = SubmitField()


@app.route('/', methods=['GET', 'POST'])
def index():
form = Form()

if request.method == 'POST':
# Getting selected options
form_data = form.select_multiple_field.data
print(form_data)

return render_template(
'index.html',
form=form,
)


if __name__ == "__main__":
app.run(debug=True)

index.html:

<!DOCTYPE html>
<html lang="en">

<head>
</head>

<body>

<style>
.dropdown-check-list {
display: inline-block;
}

.dropdown-check-list .anchor {
position: relative;
cursor: pointer;
display: inline-block;
padding: 5px 50px 5px 10px;
border: 1px solid #ccc;
}

.dropdown-check-list .anchor:after {
position: absolute;
content: "";
border-left: 2px solid black;
border-top: 2px solid black;
padding: 5px;
right: 10px;
top: 20%;
-moz-transform: rotate(-135deg);
-ms-transform: rotate(-135deg);
-o-transform: rotate(-135deg);
-webkit-transform: rotate(-135deg);
transform: rotate(-135deg);
}

.dropdown-check-list .anchor:active:after {
right: 8px;
top: 21%;
}

.dropdown-check-list ul.items {
padding: 2px;
display: none;
margin: 0;
border: 1px solid #ccc;
border-top: none;
}

.dropdown-check-list ul.items li {
list-style: none;
}

.dropdown-check-list.visible .anchor {
color: #0094ff;
}

.dropdown-check-list.visible .items {
display: block;
}
</style>

<form action="{{ url_for('index') }}" method="POST">
{{ form.hidden_tag() }}
<div id="list1" class="dropdown-check-list" tabindex="100">
<span class="anchor">Select Fruits</span>

{{ form.select_multiple_field(class="items") }}

</div>
{{ form.submit }}
</form>

<script>
var checkList = document.getElementById('list1');
checkList.getElementsByClassName('anchor')[0].onclick = function (evt) {
if (checkList.classList.contains('visible'))
checkList.classList.remove('visible');
else
checkList.classList.add('visible');
}
</script>

</body>

</html>

enter image description here

==========================================

(这个依赖于一些 JQuery 库,但感觉与您正在寻找的东西很接近)

link to that library

ma​​in.py:

from flask import Flask, render_template, request
from flask_wtf import FlaskForm
from wtforms.fields import SelectMultipleField, SubmitField

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret_key'


choices = [
'apple',
'banana',
'cherry',
]


class Form(FlaskForm):
global choices
select_multiple_field = SelectMultipleField(choices=choices)
submit = SubmitField()


@app.route('/', methods=['GET', 'POST'])
def index():
form = Form()

if request.method == 'POST':
# Getting selected options
form_data = form.select_multiple_field.data
print(form_data)

return render_template(
'index.html',
form=form,
)


if __name__ == "__main__":
app.run(debug=True)

index.html:

<!DOCTYPE html>
<html lang="en">

<head>
<link href="https://unpkg.com/multiple-select@1.5.2/dist/multiple-select.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<script src="https://unpkg.com/multiple-select@1.5.2/dist/multiple-select.min.js"></script>
</head>

<body>

<style>
select {
width: 20%;
}
</style>

<form action="{{ url_for('index') }}" method="POST">
{{ form.hidden_tag() }}
{{ form.select_multiple_field(**{"multiple": "multiple"}) }}
{{ form.submit }}
</form>

<script>
$(function () {
$('select').multipleSelect({
multiple: true,
multipleWidth: 60
})
})
</script>

</body>

</html>

enter image description here

==========================================

(如果这个答案不是您要找的,请随时与我联系并说明原因,我会尝试调整它:D)

关于python - 显示 wtforms SelectMultipleField 显示为下拉列表而不是列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70563907/

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