need selected radio/boolean fields input submitted to email

Hi I built a new project form that a user can submit which goes to my email. Currently wondering how to get only the input labels of the boolean fields the user selected (=True) sent to my email, rather than all of them. Would that involve a for loop for wtype1-5 : if True, submit. else: no submit ? For example, using: form.wtype1.data for Optional Boolean fields returns True or False. I want only the labels of True inputs. Using form.wtype1.label returns an ugly string with HTML label tags, so I had to resort to form.wtype1.description, which isn't a solution because I'm getting all values not just True ones.

test email

From: Richard salad beeeeeeetetetetet@asdf.bnt

Static Blog Ecommerce Forums Other

Basic

Basic Template 7

This is a comments area of basic test form for input functionality

form class and function

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

To change the least amount of your code but make it do what you want it to do, I would do:

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

EDIT : I made an error, but it is fixed now. I also had to make a minor modification to the form. Tested it and it works.

You may also want to remove the Length() validator from the email field (a@a.com would get rejected, for example and it already checks for a valid email) as well as changing your name field to just be Required() (some people have abnormal names).

链接地址: http://www.djcxy.com/p/61762.html

上一篇: Flask WTForms显示字段标题仅限按钮

下一篇: 需要选择提交给电子邮件的收音机/布尔字段输入