我正在使用Flask和Jinja2,我需要制作一个包含多行的可编辑表格.
这就是表格的样子:

这是HTML的代码:
<form action="/support/team-members-update" method="post">
<table>
<tbody><tr>
<th>Name</th>
<th>Id</th>
<th>Inbox Share</th>
</tr>
<tr>
<td>Ben</td><td>55555</td><td><input type="text" name="share_55555" value="0"></td></tr> <tr>
<td>Steve</td><td>66666</td><td><input type="text" name="share_66666" value="1"></td></tr> <tr>
<td>Harry</td><td>77777</td><td><input type="text" name="share_77777" value="1"></td></tr> <tr>
<td>Sally</td><td>88888</td><td><input type="text" name="share_88888" value="1"></td></tr></tbody></table>
<button type="submit">Send</button>
</form>
Run Code Online (Sandbox Code Playgroud)
我目前的实现是在Lua中,我很难编写一堆字符串并将后期数据连接到本机Lua类型(有趣!).如果必须的话,我也可以在Python中手工处理表单数据,但我想可能有更好的解决方案.
我已经对WTForms进行了一些探索,但是没有太多运气让它正常工作.
我确实找到了FieldList,但这似乎处理了相同字段的列表,而不是具有相同确切字段的多行.
我也找到了TableWidget,但是文档很稀疏,我无法弄清楚如何实现它以了解是否可以做我想做的事情.
如果选中复选框,我正在使用自定义验证器来检查字段是否为空.它检查正确,但无论它是否总是在验证值是否为数字.
基本上我需要一个字段来停止在表单的某些条件下进行验证.
有没有办法让自定义验证器停止对该字段的验证?
在我当前的项目中,我的索引页面显示了几个 torrent,每个 torrent 都有一个启动或停止 torrent 的按钮。
我使用表单和循环创建页面,因此表单始终具有相同的名称。但我需要知道用户单击了哪个按钮才能知道要停止哪个 torrent!
这是模板:
{% block content %}
<h1>Hello, {{user}} !</h1>
{% for torrent in torrents %}
<form action="/index" method="post" name="torrent">
{{torrent.control.hidden_tag()}}
<p><a href='/torrent/{{torrent.id}}'>{{torrent.name}}</a> is {{torrent.status}} and {{torrent.progress}} % finished. <input type="submit" value="Start / Stop">
</p>
</form>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
这是视图:
@app.route('/index', methods = ['GET', 'POST'])
def index():
user = 'stephane'
torrents = client.get_torrents()
# for each torrent, we include a form which will allow start or stop
for torrent in torrents:
torrent.control = TorrentStartStop() …Run Code Online (Sandbox Code Playgroud) 我有一个动态网页,应该处理两种形式:登录表单和注册表单.我正在使用WTForms来处理这两种形式,但是我在使它工作时遇到了一些麻烦,因为两种形式都被渲染到同一页面.
以下是我的网页登录表单的代码:
PYTHON:
class Login(Form):
login_user = TextField('Username', [validators.Required()])
login_pass = PasswordField('Password', [validators.Required()])
@application.route('/index', methods=('GET', 'POST'))
def index():
l_form = Login(request.form, prefix="login-form")
if request.method == 'POST' and l_form.validate():
check_login = cursor.execute("SELECT * FROM users WHERE username = '%s' AND pwd = '%s'"
% (l_form.login_user.data, hashlib.sha1(l_form.login_pass.data).hexdigest()))
if check_login == True:
conn.commit()
return redirect(url_for('me'))
return render_template('index.html', lform=l_form)
HTML:
<form name="lform" method="post" action="/index">
{{ lform.login_user }}
{{ lform.login_pass }}
<input type="submit" value="Login" />
</form>
Run Code Online (Sandbox Code Playgroud)
以下是我的网页注册表格的代码: …
这是我的HTML下拉菜单.该值是子表的主键.
<select id="category" name="category">
<option selected value="__None"></option>
<option value="1">Category Number One</option>
<option value="2">Category Number Two</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我需要使用值整数1而不是"类别编号一"更新Post.category_id.这是我的代码.
# create new post
@app.route('/admin/post', methods=['GET', 'POST'])
@login_required # Required for Flask-Security
def create_post():
form = PostForm()
if form.validate_on_submit():
post = Post(title=form.title.data,
body=form.body.data,
pub_date=form.pub_date.data,
cateogry_id=form.category.data)
db.session.add(post)
db.session.commit()
flash('Your post has been published.')
return redirect(url_for('admin'))
posts = Post.query.all()
return render_template('create_post.html', form=form, posts=posts)
Run Code Online (Sandbox Code Playgroud)
我试过制作......
cateogry_id=form.category.data
cateogry_id=form.category.value
Run Code Online (Sandbox Code Playgroud)
现在不是那么好!
我有这个订单表格,允许我的用户创建订单.订单由多个元组组成(producetype, quantity).Producetype应该以<select>表单形式呈现,而数量只能是输入.应该动态添加producetype的选择,因为这可能会改变.目前,我用裸HTML编写了这个

我想使用WTForm,因为WTForm真的简化了我的代码.但是,我无法这样做:
码:
class OrderEntryForm(Form):
quantity = IntegerField('Quantity',
[validators.Required(), validators.NumberRange(min=1)])
# we will be dynamically adding choices
producetype = SelectField('Produce',
[validators.Required()],
choices=[])
class OrderForm(Form):
name = TextField('Crop', [validators.Length(min=3, max=60)])
due_date = DateField('Due Date', [validators.required()])
order_entries = FieldList(FormField(OrderEntryForm))
Run Code Online (Sandbox Code Playgroud)
我有以下问题:
order_entriesOrderForm 的字段中?order = { name:hello, due_date:2014-06-18, order_entries:[ {producetype_id: 1, quantity: 2}, {producetype_id: 3, quantity: 4}] }怎么能用OrderForm正确的OrderEntryForm价值填充我?我的代码可以在这里找到:https://gist.github.com/vicngtor/f8c8f0519dbd6b3b6110
我想将"data-"属性添加到表单字段以与Bootstrap集成.我在模板中尝试了以下内容:
{{ form.test(data-toggle="toggle", data-size="mini", data-on="Yes", data-off="No", type="checkbox")}}
Run Code Online (Sandbox Code Playgroud)
并得到此错误:
TemplateSyntaxError: expected token ',', got '='
Run Code Online (Sandbox Code Playgroud)
为什么我会收到此错误,如何解决?
我通过Flask-WTF非常简化的wiki阅读,并且无法理解我能用它做什么.我的印象是<form>html 的部分现在只能看起来像
<form method="post">
{{ form.hidden_tag() }}
{{ form.name }}
<input type="submit">
</form>
Run Code Online (Sandbox Code Playgroud)
但我真的想要使用物化的样式,例如:
<div class="row">
<div class="input-field col s6">
<i class="material-icons prefix">account_circle</i>
<input value="FN" id="first_name" type="text" class="validate">
<label class="active" for="first_name">First Name</label>
</div>
<div class="input-field col s6">
<input value="LN" id="last_name" type="text" class="validate">
<label class="active" for="last_name">Last Name</label>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我在哪里能适应{{ form.first_name }}并{{ form.last_name }}进入?
编辑:让我详细说明我的答案:例如,我想要像Materialized datepicker(一个很好的弹出日历,让用户选择日期),这应该在<input class='datepicker' length="50">,但现在我正在替换整<input>行与{{ form.date }}...我失去了编辑课程的特权,但没有.
有谁知道datalist在 Flask 中使用 WTForms创建字段的方法吗?
我知道如何创建一个,SelectField但如果它不在列表中,我需要允许用户输入他们自己的值。
这就是我想做的http://www.w3schools.com/tags/tag_datalist.asp
谢谢
我有一个 WTF SelectField,我正在尝试存储用户选择的名称以在另一个页面上显示。
鉴于我的表格是
choice = SelectField('Choice', choices=[('cho_1', 'Choice One'), ('cho2', 'Choice Two')])
Run Code Online (Sandbox Code Playgroud)
我明白那个
self.choice = form.choice.data
Run Code Online (Sandbox Code Playgroud)
将让我获得用户的选择(比如 cho_1),但是我如何获得价值(“选择一个”)?我觉得这对 dicts 来说很简单,但是到目前为止,各种尝试加上谷歌搜索/搜索都没有帮助。