Hou*_*man 5 flask wtforms flask-wtforms
在Django中,您有一个名为Formsets的多表单功能,您可以使用它在同一模板中创建多个表单.我试图在Flask/WTforms中实现类似的功能.
<form action="{{ url_for('request-accept') }}" method='post'>
<table>
<tbody>
{% for request in requests %}
<tr>
<td>
<div class="person-header">
<img src="{{request.profile_pic_url}}" class="img-circle profile-image"/>
<p class="person-header-text">{{request.fullname()}}</p>
</div>
</td>
<td>
<input type="checkbox" id="{{request.key.urlsafe()}}" name="checkbox{{loop.index}}">
</td>
</tr>
{% endfor %}
</tbody>
</table>
<input class='submit btn btn-primary' type=submit value="Connect">
</form>
Run Code Online (Sandbox Code Playgroud)
我们的想法是拥有一个包装所有复选框的表单,用户喜欢勾选这些复选框.目前看来我并没有在Flask中生成任何表单类,因为我不知道如何创建动态FormSet,因此我在html中动态创建表单.
但需要注意的是,我不知道如何user id通过复选框检索所选内容.(我把它存放在id因为我不知道更好)
但是我无法访问id request.values['checkbox1'].我只能看看它on或off.
有任何建议请问如何解决?
Sea*_*ira 11
你的问题是,id不发送回服务器-仅value是...因为你的复选框不会有一个value属性使用默认值,这恰好是on.
既然你这个标记与wtforms,我给你的,你怎么一个例子可以做这个.
WTForms的文档有一个示例类,它将为您创建一个复选框列表:
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()
Run Code Online (Sandbox Code Playgroud)
您可以这种方式在自定义表单中使用此字段:
class FriendsForm(Form):
potential_friends = MultiCheckboxField("Friends", coerce=int)
# ... later ...
@app.route("/add-friends", methods=["GET", "POST"])
def add_friends():
form = FriendsForm(request.form)
# lookup friends, for now we'll use a static list
form.potential_friends.choices = [(1, "Sam"), (2, "Joe")]
# We'll also need a mapping of IDs to Person instances
# (Made up for this example - use your own ;-) )
mapping = {
1: Person("Sam", profile_pic="sam.jpg"),
2: Person("Joe", profile_pic="joe.png")
}
if request.method == "POST":
# Mark new friends
return render_template("friends.html", form=form, persons=mapping)
Run Code Online (Sandbox Code Playgroud)
然后,friends.html你可以遍历该form.potential_friends字段:
{% for person in form.potential_friends %}
persons[person.data].profile_pic :: {{person.label}} :: {{person}}<br>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
您可以在for循环内自定义HTML .我的特定示例应该呈现(还有一些属性,比如for和name):
sam.jpg :: <label>Sam</label> :: <input type="checkbox" value="1">
joe.png :: <label>Joe</label> :: <input type="checkbox" value="2">
Run Code Online (Sandbox Code Playgroud)
我个人会hidden在每个复选框下的字段集中添加一个输入字段,其名称如“friend_nametag1”以及与朋友 ID 相对应的值。每个“朋友”都会增加 1。因此,您可以使用类似的东西在烧瓶视图中查找它
friend_list = []
list_of_checkboxes = ... (fetch from request.form... ?)
dict_of_friend_nametags = ... (build from request.form... ?)
if 'checkbox1' in list_of_checkboxes:
friend_list.append(dict_of_friend_nametags.get('friend_nametag1')
Run Code Online (Sandbox Code Playgroud)
显然,您可以使用某种逻辑来拥有增量索引(在本例中为“checkbox1”中的“1”)。
我对 WTForms 不太熟悉,因此可能有更好的方法来执行此操作,但此解决方案使用当前代码实现起来相当简单。
如果您想要 FieldSet 或 FormSet,我建议您将 FormField 与 FieldList 以及相关文档结合使用:http ://wtforms.simplecodes.com/docs/dev/fields.html#field-enclosures
ps:我不建议request在模板或代码中使用变量名,因为它可能会影响requestflask的全局?XD