title: How can I persist html form values using Flask?
body: <p>I'm using Flask and doing a contact form. How can I persist form data when returning back to the same view after a user submits the form? I think I've been able to do this in the past using WTForms, but is there another way to do it? WTForms seems overkill for just a contact form. I've tried doing form = request.form right after line 29 and then passing form = form as a variable in render_template(). Obviously, didn't work. If this is only possible using WTForms what is the simplest way to do it? I think writing macros for html element displays is just too much and adds clutter.</p>
  
  <pre><code> @app.route('/contact',methods=['GET','POST'])
   def contact():
       error = None
       if request.method == 'POST':
           if request.form['captcha'] == session["captcha"]:
               # Message(subject,sender,recipients_list)
               msg = Message(request.form['subject'],
                               sender=request.form['email'],
                               recipients=["email@gmail.com"])
               msg.body = request.form['message']
               flash('Message sent')
               mail.send(msg)
           else:
               flash('Invalid captcha')
       session["captcha"] = 'test'
       return render_template('contact.html',page_header='Contact')
  </code></pre>
  
  <p>contact.html:</p>
  
  <pre><code> {% extends "base.html" %}
    {% block body %}
  
    &lt;div class="row"&gt;
        &lt;div class="col-md-10"&gt;
            {% with messages = get_flashed_messages() %}
                {% if messages %}
                    &lt;ul class=flashes&gt;
                    {% for message in messages %}
                        &lt;li&gt;{{ message }}&lt;/li&gt;
                    {% endfor %}
                    &lt;/ul&gt;
                {% endif %}
            {% endwith %}
            &lt;form action="{{ url_for('contact') }}" method="POST"&gt;
                &lt;div class="form-group"&gt;
                    &lt;label for="email"&gt;Email&lt;/label&gt;
                    &lt;input type="email" class="form-control" id="email" name="email" placeholder="Your email address"&gt;
                &lt;/div&gt;
                &lt;div class="form-group"&gt;
                    &lt;label for="subject"&gt;Subject&lt;/label&gt;
                    &lt;input type="text" class="form-control" id="subject" name="subject" placeholder="Subject"&gt;
                &lt;/div&gt;
                &lt;div class="form-group"&gt;
                    &lt;label for="message"&gt;Message&lt;/label&gt;
                    &lt;textarea class="form-control" id="message" name="message" placeholder="Your message"&gt;&lt;/textarea&gt;
                &lt;/div&gt;
                &lt;div class="form-group"&gt;
                    &lt;label for="captcha"&gt;Captcha&lt;/label&gt;
                    &lt;input type="text" class="form-control" id="captcha" name="captcha"&gt;
                &lt;/div&gt;
                &lt;button type="submit" class="btn btn-primary pull-right"&gt;Submit&lt;    /button&gt;
            &lt;/form&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;br&gt;
    {% endblock %}
  </code></pre>