需要选择提交给电子邮件的无线电/布尔字段输入 - wtforms + flask

时间:2016-11-30 01:31:48

标签: flask wtforms

您好我构建了一个新的项目表单,用户可以将其提交到我的电子邮件中。目前想知道如何只获取用户选择(= True)发送到我的电子邮件的布尔字段的输入标签,而不是全部。这会涉及wtype1-5的for循环:如果为True,则提交。否则:没有提交?例如,使用:form.wtype1.data for Optional Boolean字段返回True或False。我只想要True输入的标签。使用form.wtype1.label返回一个带有HTML标签标签的丑陋字符串,所以我不得不求助于form.wtype1.description,这不是解决方案,因为我得到的所有值都不仅仅是真值。

测试电子邮件

来自:Richard salad 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()

1 个答案:

答案 0 :(得分:1)

要更改最少量的代码但让它按照您的意愿执行,我会这样做:

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()

修改: 我犯了一个错误,但现在已修复了。我还必须对表单进行一些小修改。测试它,它的工作原理。

您可能还想从Length()字段中移除email验证程序(例如a@a.com会被拒绝,并且已经检查了有效的电子邮件)以及更改了name字段只是Required()(有些人的姓名不正确)。