需要选择提交给电子邮件的收音机/布尔字段输入
嗨,我建立了一个新的项目表格,用户可以提交到我的电子邮件。 目前想知道如何只将用户选择的布尔字段的输入标签(= True)发送到我的电子邮件,而不是全部。 那会涉及wtype1-5的for循环:如果为True,则提交。 其他:没有提交? 例如,对可选布尔字段使用:form.wtype1.data返回True或False。 我只想要True输入的标签。 使用form.wtype1.label会返回一个带有HTML标签标签的丑陋字符串,所以我不得不求助于form.wtype1.description,这不是一个解决方案,因为我得到的不仅仅是真正的值。
测试邮件
来自:理查德沙拉beeeeeeetetetetet@asdf.bnt
静态博客电子商务论坛其他
基本
基本模板7
这是输入功能基本测试表单的注释区域
形式类和功能
class NewProjectForm(Form):
subject = 'New Project Request!'
name = TextField([validators.Length(min=5, max=20)])
email = TextField([validators.Length(min=8, max=50), validators.Email()])
wtype1 = BooleanField(validators=[validators.Optional()], label='Static', description='Static')
wtype2 = BooleanField(validators=[validators.Optional()], label='Blog', description='Blog')
wtype3 = BooleanField(validators=[validators.Optional()], label='Ecommerce', description='Ecommerce')
wtype4 = BooleanField(validators=[validators.Optional()], label='Forums', description='Forums')
wtype5 = BooleanField(validators=[validators.Optional()], label='Other', description='Other')
wtype = (wtype1,wtype2,wtype3,wtype4,wtype5)
wpack = RadioField([validators.Required()], choices=[('SkyFly','SkyFly'),('Basic','Basic'),('Standard','Standard'),('Intermediate','Intermediate')])
templates = RadioField([validators.Required()], choices=[('Basic Template 1','Basic Template 1'),('Basic Template 2','Basic Template 2'),
('Basic Template 3','Basic Template 3'), ('Basic Template 4','Basic Template 4'),('Basic Template 5','Basic Template 5'),
('Basic Template 6','Basic Template 6'),('Basic Template 7','Basic Template 7'),('Basic Template 8','Basic Template 8'),
('Basic Template 9','Basic Template 9'),('Basic Template 10','Basic Template 10'),
('Intermd. Template 1', 'Intermd. Template 1'),('Intermd. Template 2','Intermd. Template 2')])
comments = TextAreaField([validators.Length(min=10, max=400)])
@app.route('/dashboard/newproject/', methods=["GET","POST"])
def new_project():
try:
form = NewProjectForm()
if request.method == "POST":
if form.validate() == False:
flash('Invalid! All fields are required.')
return render_template("dashboardNewproject.html", form=form)
else:
msg = Message(form.subject, sender='form.email.data', recipients=['skyldev1@gmail.com'])
msg.body = """
From: %s %s n
%s %s %s %s %s n %s n %s n n %s
""" % (form.name.data, form.email.data, form.wtype1.description, form.wtype2.description,
form.wtype3.description, form.wtype4.description, form.wtype5.description,
form.wpack.data, form.templates.data, form.comments.data)
mail.send(msg)
flash('Congratulations! A new project has been submitted.')
gc.collect()
要改变你的代码的最小数量,但要使它做你想做的事情,我会这样做:
class NewProjectForm(Form):
subject = 'New Project Request!'
name = TextField([validators.Length(min=5, max=20)])
email = TextField([validators.Length(min=8, max=50), validators.Email()])
wtype1 = BooleanField(validators=[validators.Optional()], label='Static', description='Static')
wtype2 = BooleanField(validators=[validators.Optional()], label='Blog', description='Blog')
wtype3 = BooleanField(validators=[validators.Optional()], label='Ecommerce', description='Ecommerce')
wtype4 = BooleanField(validators=[validators.Optional()], label='Forums', description='Forums')
wtype5 = BooleanField(validators=[validators.Optional()], label='Other', description='Other')
wtype = (wtype1,wtype2,wtype3,wtype4,wtype5)
wpack = RadioField([validators.Required()], choices=[('SkyFly','SkyFly'),('Basic','Basic'),('Standard','Standard'),('Intermediate','Intermediate')])
templates = RadioField([validators.Required()], choices=[('Basic Template 1','Basic Template 1'),('Basic Template 2','Basic Template 2'),
('Basic Template 3','Basic Template 3'), ('Basic Template 4','Basic Template 4'),('Basic Template 5','Basic Template 5'),
('Basic Template 6','Basic Template 6'),('Basic Template 7','Basic Template 7'),('Basic Template 8','Basic Template 8'),
('Basic Template 9','Basic Template 9'),('Basic Template 10','Basic Template 10'),
('Intermd. Template 1', 'Intermd. Template 1'),('Intermd. Template 2','Intermd. Template 2')])
comments = TextAreaField([validators.Length(min=10, max=400)])
def wtype(self):
return (self.wtype1,self.wtype2,self.wtype3,self.wtype4,self.wtype5)
@app.route('/dashboard/newproject/', methods=["GET","POST"])
def new_project():
try:
form = NewProjectForm(request.form)
if request.method == "POST":
if form.validate() == False:
flash('Invalid! All fields are required.')
return render_template("dashboardNewproject.html", form=form)
else:
wtype = ''
for each in form.wtype():
if each.data:
wtype+=' '+each.description
msg = Message(form.subject, sender='form.email.data', recipients=['skyldev1@gmail.com'])
msg.body = """
From: %s %s n
%s n %s n %s n n %s
""" % (form.name.data, form.email.data, wtype[1:],
form.wpack.data, form.templates.data, form.comments.data)
mail.send(msg)
flash('Congratulations! A new project has been submitted.')
gc.collect()
编辑 :我犯了一个错误,但它现在已经修复。 我也不得不对表单做一些小修改。 测试它,它的工作原理。
您可能还想从email
字段中删除Length()
验证器(例如a@a.com将被拒绝,并且它已经检查了有效的电子邮件)以及将您的name
字段更改为仅Required()
( Required()
(有些人有异常的名字)。
上一篇: need selected radio/boolean fields input submitted to email