richard0096 richard0096 - 1 day ago 4
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')


Template:

<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.name(class_="form-control") }} <!-- this creates the name form field -->
<br>
<label for="address">Address:</label>
{{ form.address(class_="form-control", rows='5', cols='40') }} <!-- this creates the adress form field -->
<br>
<label for="email">E-mail Address:</label>
{{ form.email(class_="form-control") }}
<br>
<label for="telephone">Phone Number: </label>
{{ form.telephone(class_="form-control") }}
<br>
<label for="file_upload">Upload CV: </label>
{{ form.file_upload(class_="form-control") }}
<br>
<label for="Department">Department:</label>
{{ form.department(class_="form-control")}}
<br>
</select>
</div>
<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():
flash('Success!')
return redirect('index')
print(form.errors)
return render_template('index.html',
title='Application Form',
form=form)

Answer

Each field has a data attribute containing the processed data.

the_email = form.email.data
Comments