richard0096 richard0096 - 1 year ago 61
Python Question

How do I reference the data a user enters into my form?

I want to extract the e-mail address a user enters into my e-mail field. I'm not too sure how to go about this? I want to use this e-mail address to send an e-mail containing the file that has also been entered into my form.

Here is my form for reference:

class ApplicationForm(Form):
name = StringField('name', validators=[DataRequired()], render_kw={"placeholder": "What is your name?"})
address = TextAreaField('address', validators=[DataRequired()], render_kw={"placeholder": "What is your address?"})
telephone = IntegerField('telephone', render_kw={"placeholder": "What is your phone number?"})
email = StringField('email',validators=[DataRequired(), Email()], render_kw={"placeholder": "What is your e-mail address?"})
department = SelectField('department', coerce=str,choices=[('esb','ESB'),('mds','MDS'),('cpoms','CPOMS'),('flex','Flex')] , validators=[DataRequired()])
file_upload = FileField('file_upload')


<form enctype="multipart/form-data" action="/index" method="post" role="form"> <!-- how the data is obtained from the form (POST method) -->
{{ form.csrf_token }}
<div class="form-group">
<label style="margin-top: 10px;" for="name">Name:</label>
{{"form-control") }} <!-- this creates the name form field -->
<label for="address">Address:</label>
{{ form.address(class_="form-control", rows='5', cols='40') }} <!-- this creates the adress form field -->
<label for="email">E-mail Address:</label>
{{"form-control") }}
<label for="telephone">Phone Number: </label>
{{ form.telephone(class_="form-control") }}
<label for="file_upload">Upload CV: </label>
{{ form.file_upload(class_="form-control") }}
<label for="Department">Department:</label>
{{ form.department(class_="form-control")}}
<button name="submit" type="submit" class="btn btn-primary">Submit</button> </form>

Here is my view:

@app.route('/index', methods=['GET', 'POST'])
def index():
errors = ''

form = ApplicationForm(request.form)

if form.is_submitted():
print "Form successfully submitted"

if form.validate_on_submit():
return redirect('index')
return render_template('index.html',
title='Application Form',

Answer Source

Each field has a data attribute containing the processed data.

the_email =
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download