将烧瓶形式自定义为 table
Customize flask form as table
我在这个问题上苦苦思索了很长一段时间。在我的 Flask 应用程序中,我目前有一个产品数据库,在应用程序中,我有一个页面将每个产品列查询到 table。
例如,我有产品 1234
,我可以在 example.com/items/1234
中查看其详细信息(即数据库列),这会给我以下信息:
<div class="container">
<div class="content">
<a href="/item">Back</a>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Detail-1<th>
<td>Example</td>
</tr>
<tr>
<th scope="row">Detail-2</th>
<td>Example</td>
</tr>
我遇到的问题如下:
我希望能够添加具有相同 table 风格的新产品。为此,我创建了如下表格:
class NewArticleForm(FlaskForm):
detail_1 = StringField("Detail-1")
detail_2 = IntegerField("Detail-2")
...
submit = SubmitField("Submit")
除了 {{ wtf.quick_form(form) }}
,我现在完全不知道如何自定义模板中的表单外观。我尝试的是以下内容:
<form method="POST">
<table class="table">
{% for name in form %}
<tr>
<th scope="row">{{ name.label }}</th>
<td>{{ name }}</td>
</tr>
{% endfor %}
</table>
</form>
table 看起来不错(至少如此),但我认为请求没有正确发送。该页面使用 "POST /url HTTP/1.1"
正确加载,但似乎无法正确加载。
我的意思是虽然请求发送正确,但我可以看到我是否通过 Flask 服务器 运行 应用程序。但是,似乎没有任何内容传输到数据库。该页面只是重新加载,输入的数据仍在字段中,没有任何内容传输到数据库。如果我只是使用 wtf.quick_form
那么数据就会正确地发送到数据库。
那么如何正确自定义表单外观and/or我错过了关键步骤吗?
其实这很尴尬。我设法自己解决了这个问题,天哪,这是一个愚蠢的解决方案。
没有任何内容传输到数据库的原因是,如果某些字段留空(在表单中未标记为必填),它们将被解释为字符串字段,这当然会导致 Integer- 错误领域。
问题是,正如您在我的模板代码中看到的那样,我没有包含在提交字段时显示任何错误的代码。我将模板代码更改为:
{% macro render_field(field) %}
<th scope="row">{{ field.label }}
<td>{{ field(**kwargs)|safe }}
{% if field.errors %}
<ul class=errors>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
{% endif %}
{% endmacro %}
<form method="POST">
<table class="table">
{{ form.csrf_token }}
{% for name in form %}
<tr>
{{ render_field(name) }}
</tr>
{% endfor %}
</table>
</form>
我现在会羞愧地低下头,希望有一天这能对某人有所帮助。
我在这个问题上苦苦思索了很长一段时间。在我的 Flask 应用程序中,我目前有一个产品数据库,在应用程序中,我有一个页面将每个产品列查询到 table。
例如,我有产品 1234
,我可以在 example.com/items/1234
中查看其详细信息(即数据库列),这会给我以下信息:
<div class="container">
<div class="content">
<a href="/item">Back</a>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Detail-1<th>
<td>Example</td>
</tr>
<tr>
<th scope="row">Detail-2</th>
<td>Example</td>
</tr>
我遇到的问题如下:
我希望能够添加具有相同 table 风格的新产品。为此,我创建了如下表格:
class NewArticleForm(FlaskForm):
detail_1 = StringField("Detail-1")
detail_2 = IntegerField("Detail-2")
...
submit = SubmitField("Submit")
除了 {{ wtf.quick_form(form) }}
,我现在完全不知道如何自定义模板中的表单外观。我尝试的是以下内容:
<form method="POST">
<table class="table">
{% for name in form %}
<tr>
<th scope="row">{{ name.label }}</th>
<td>{{ name }}</td>
</tr>
{% endfor %}
</table>
</form>
table 看起来不错(至少如此),但我认为请求没有正确发送。该页面使用 "POST /url HTTP/1.1"
正确加载,但似乎无法正确加载。
我的意思是虽然请求发送正确,但我可以看到我是否通过 Flask 服务器 运行 应用程序。但是,似乎没有任何内容传输到数据库。该页面只是重新加载,输入的数据仍在字段中,没有任何内容传输到数据库。如果我只是使用 wtf.quick_form
那么数据就会正确地发送到数据库。
那么如何正确自定义表单外观and/or我错过了关键步骤吗?
其实这很尴尬。我设法自己解决了这个问题,天哪,这是一个愚蠢的解决方案。
没有任何内容传输到数据库的原因是,如果某些字段留空(在表单中未标记为必填),它们将被解释为字符串字段,这当然会导致 Integer- 错误领域。
问题是,正如您在我的模板代码中看到的那样,我没有包含在提交字段时显示任何错误的代码。我将模板代码更改为:
{% macro render_field(field) %}
<th scope="row">{{ field.label }}
<td>{{ field(**kwargs)|safe }}
{% if field.errors %}
<ul class=errors>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
{% endif %}
{% endmacro %}
<form method="POST">
<table class="table">
{{ form.csrf_token }}
{% for name in form %}
<tr>
{{ render_field(name) }}
</tr>
{% endfor %}
</table>
</form>
我现在会羞愧地低下头,希望有一天这能对某人有所帮助。