Python 将TextAreaField(用于规格)放在带有烧瓶WTF的无线电场中
我正在尝试用flask和flask wtf构建一个简单的Web表单 在我的表格中,我要求提供所需的分段类型(选项:通用、自动和自定义)。如果类型是自定义的,我需要获得一个带有规范的特殊字段 下面是我的代码。它正在起作用,但我觉得读起来不舒服 在forms.py中Python 将TextAreaField(用于规格)放在带有烧瓶WTF的无线电场中,python,html,forms,flask,flask-wtforms,Python,Html,Forms,Flask,Flask Wtforms,我正在尝试用flask和flask wtf构建一个简单的Web表单 在我的表格中,我要求提供所需的分段类型(选项:通用、自动和自定义)。如果类型是自定义的,我需要获得一个带有规范的特殊字段 下面是我的代码。它正在起作用,但我觉得读起来不舒服 在forms.py中 from flask_wtf import Form from wtforms import StringField, RadioField, BooleanField, TextAreaField from wtforms impor
from flask_wtf import Form
from wtforms import StringField, RadioField, BooleanField, TextAreaField
from wtforms import validators
class requestSettings(Form):
typeOfSegmentation = RadioField('typeOfSegment',
choices=[('generic', 'by generic persona'), \
('auto', 'by automatic clustering'),
('custom', 'by manual definition')
],
default='generic',
validators=[validators.DataRequired("indicate the method to segment users")])
typeOfSegmentation_custom = TextAreaField('custom definition')
def validate(self):
is_validated = True
rv = Form.validate(self)
if not rv:
is_validated = False
if self.typeOfSegmentation.data == 'custom':
if not self.typeOfSegmentation_custom.data:
self.typeOfSegmentation_custom.errors.append("Please specify the way you define your segments")
is_validated = False
return is_validated
在我的HTML中
<!DOCTYPE html>
<html>
<head>
</head>
<body>
Your request is {{ session_id }} <br>
{% for field, errors in form.errors.items() %}
{% for error in errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
{% endfor %}<br>
{% block content %}
<h1>Segmentation</h1>
<form action="" method="post" name="name">
<p>Select the method to segment user:
<table>
<tr>
{% for subfield in form.typeOfSegmentation %}
<tr>
<td>{{ subfield }}</td>
<td>{{ subfield.label }}</td>
{% if loop.index0 == 2 %}
<td > Enter manual description {{form.typeOfSegmentation_custom}}
</td>
{% endif %}
</tr>
{% endfor %}
</table>
</form>
{% endblock %}
</body>
</html>
您的请求是{{session\u id}}
{%为字段,错误形式为.errors.items()%}
{错误%中的错误为%0}
[{{error}}]
{%endfor%}
{%endfor%}
{%block content%}
分段
选择分段用户的方法:
{form.typeOfSegmentation%中的子字段为%1}
{{子字段}}
{{subfield.label}
{%if loop.index0==2%}
输入手动说明{{form.typeOfSegmentation\u custom}}
{%endif%}
{%endfor%}
{%endblock%}
这是正确的方法吗?flask WTF中是否存在单选按钮+文本字段