Pre-Populate an edit form with WTForms and Flask
<p>I am able to add a new entry to my database using WTForms and Flask and I can edit too, the problem is that I need to display the already existing information in the database in an edit form.</p>

<p>I have the following code:</p>

<h1>A Class For the Edit Post Form</h1>

<pre><code>class editPostForm(Form):
    postTitle = TextField('postTitle', validators.Required()])
    postSubtitle = TextField('postSubtitle', validators.Required()])
</code></pre>

<h1>A Route for the Edit Post Template</h1>

<pre><code>@app.route('/editpost/&lt;postId&gt;', methods = ['GET','POST'])
def editpost_page(postId):
try:
    form = editPostForm(form)

    if request.method == "POST" and form.validate():
        postTitle = form.postTitle.data
        postSubtitle = form.postSubtitle.data

        c, conn = connection()

        query = c.execute("SELECT * FROM posts WHERE post_id = (%s)",
                          [noinjection(postId)])

        c.execute("UPDATE posts SET post_title=%s, post_subtitle=%s WHERE post_id = %s",
                     [
                     noinjection(postTitle),
                     noinjection(postSubtitle),
                     noinjection(postId)
                     ])

        conn.commit()

        flash("Post Edited", 'success')

        c.close()
        conn.close()
        gc.collect()

        return redirect(url_for('posts'))

    return render_template("editpost.html", form = form, POST_ID = postId)

except Exception as e:
    return(str(e))
</code></pre>

<h1>The Edit Post Template {jinja}</h1>

<pre><code>{% extends "header.html" %}

{% block body %}

&lt;body&gt;
    &lt;div class="container-fluid"&gt;
        &lt;br /&gt;
        &lt;h4&gt;Edit Post&lt;/h4&gt;
        &lt;br /&gt;
        {% from "_formhelpers.html" import render_field %}
        &lt;form action="/editpost/{{ POST_ID }}" class="form-horizontal" method="post"&gt;
            {% from "_formhelpers.html" import render_field %}
            &lt;form action="/editpost/" method="post"&gt;
              &lt;div class="form-group"&gt;
                &lt;label for="postTitle"&gt;Post Title&lt;/label&gt;
                &lt;input type="text" class="form-control" id="postTitle" name="postTitle" placeholder="Post Title" value="{{request.form.postTitle}}"&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                &lt;label for="postSubtitle"&gt;Post Subtitle&lt;/label&gt;
                &lt;input type="text" class="form-control" id="postSubtitle" name="postSubtitle" placeholder="Post Subtitle" value="{{request.form.postSubtitle}}"&gt;
              &lt;/div&gt;
              &lt;button type="submit" class="btn btn-default"&gt;Submit&lt;/button&gt;
            &lt;/form&gt;
            {% if error %}
                &lt;p class="error"&gt;&lt;strong&gt;Error: &lt;/strong&gt;{{error}}&lt;/p&gt;
            {% endif %}
        &lt;/form&gt;
        {% if error %}
            &lt;p class="error"&gt;&lt;strong&gt;Error: &lt;/strong&gt;{{error}}&lt;/p&gt;
        {% endif %}

    &lt;/div&gt;
&lt;/body&gt;

{% endblock %}
</code></pre>

<p>With the following code, I am getting a selected post to update in the database, but the editpost template is not showing the values which are already in the database and all the fields are blank. </p>

<p>How can I pre-populate the form before editing?</p>