Flask / WTForms中的动态表单(Formsets)? [英] Dynamic Forms (Formsets) in Flask / WTForms?
问题描述
< form action ={{url_for('request-accept ')}}method ='post'>
< table>
< tbody>
{%请求中的请求%}
< 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 =checkboxid ={{request.key.urlsafe()}}name =checkbox {{loop.index}}>
< / td>
< / tr>
{%endfor%}
< / tbody>
< / table>
< input class ='submit btn btn-primary'type = submit value =Connect>
< / form>
这个想法是用一种形式来包装所有的复选框,用户喜欢打勾成为朋友用。就目前而言,我并不是真的在Flask中生成任何表单类,因为我不知道如何创建一个动态FormSet,因此我在html中动态地创建了表单。
不过,我不知道如何通过复选框来检索选定的 但是我无法访问 有什么建议如何解决这个请吗? c> id 用户标识
。 (我已经将它存储在 id中,因为我不知道更好)
不会被发送回服务器 - 只有
request.values ['checkbox1']
中的id。我只能看到它的在
或 off
。
问题
value
是...因为你的复选框没有 em> a value
属性使用默认值,这恰好是在
上。
由于您使用 wtforms ,我会给你一个你如何做这个的例子。
不要再有这个问题了
WTForms的文档有一个示例这个类会为你创建一个复选框列表:
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $>类MultiCheckboxField(SelectMultipleField)
多选,除了显示复选框列表
迭代该字段将产生子字段,允许自定义渲染
封闭的复选框字段。
widget = widgets.ListWidget(prefix_label = False)
option_widget = widgets.CheckboxInput()
您可以在您的自定义表单中以这种方式使用此字段:
class FriendsForm (Form):
potential_friends = MultiCheckboxField(Friends,coerce = int)
#... later ...
@ app.route( / add-friends,methods = [GET,POST])
def add_friends():
form = FriendsForm(request.form)
#将使用一个静态列表
form.potential_friends.choices = [(1,Sam),(2,Joe)]
#我们还需要ID到Person实例
#(用这个例子 - 用你自己的;-))
mapping = {
1:Person(Sam,profile_pic =sam.jpg) ,
2:Person(Joe,profile_pic =joe.png)
}
如果request.method ==POST:
#标记新朋友
返回render_templ ate(friends.html,form = form,persons = mapping)
然后在 friends.html
你可以迭代 form.potential_friends
字段:
{for person in form.potential_friends%}
persons [person.data] .profile_pic :: {{person.label}} :: {{person}}<峰; br>
{%endfor%}
您可以在 for
循环。我的特殊例子应该呈现(具有更多的属性,比如和名称
) :
sam.jpg ::< label> Sam< / label> ::< input type =checkboxvalue =1>
joe.png ::< label> Joe< / label> ::< input type =checkboxvalue =2>
In Django you have a multiple form feature called Formsets, which you can use to create multiple forms into the same template. I am trying to achieve something similar in 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>
The idea is having one form that wrapps all the checkboxes, which the user like to tick to become friends with. As it currently stands I am not really generating any form class in Flask, since I don't know how to make a dynamic FormSet, hence I create the form dynamically inside the html.
The caveat is though, I don't know how to retrieve the selected user id
via the checkbox. (I have stored it in the id
because I didn't know better)
But I can't access the id in request.values['checkbox1']
. I can only see if its on
or off
.
Any suggestions how to solve this please?
The problem
Your problem is that id
is not sent back to the server - only value
is ... and since your checkboxes don't have a value
attribute the default value is used, which happens to be on
.
Since you tagged this with wtforms, I'll give you an example of how you could be doing this.
Never have this issue again
The WTForms' documentation has an example class that will create a list of checkboxes for you:
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()
You would use this field in your custom form in this manner:
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)
Then, in friends.html
you can iterate over the form.potential_friends
field:
{% for person in form.potential_friends %}
persons[person.data].profile_pic :: {{person.label}} :: {{person}}<br>
{% endfor %}
You can customize your HTML inside the for
loop. My particular example should render (with a few more attributes, like for
and name
):
sam.jpg :: <label>Sam</label> :: <input type="checkbox" value="1">
joe.png :: <label>Joe</label> :: <input type="checkbox" value="2">
这篇关于Flask / WTForms中的动态表单(Formsets)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!