title: Python flask - redirect to same page with query results
body: <p>I've gone through a number of other answers, but can't work out how they relate to my issue.  I'm relatively new to Python and Flask, but keen to learn.</p>
  <p>Context is that I'm adding a father and / or mother on my home page (in any order) and redirecting to the /family/ page.  Once I have that page displayed, I can add the other parent and then proceed to add the children. I can add as many children as I need, and every time I add a child's name, I want it to display in a list, with a blank pair of fields and an add button appearing after I add each child (ready for the next child).  Most of this works fine BUT I'm trying to protect against the last child forename / surname I submitted being resubmitted when I refresh the page.</p>
  <p>Here's part of my Python code:</p>
  <pre><code>@app.route(&quot;/family/&lt;parentsid&gt;&quot;, methods=[&quot;GET&quot;, &quot;POST&quot;])
  def show_family(parentsid):
  
      form = AddIndividual()
  
      if request.method == &quot;POST&quot;:
  
          if request.form.get(&quot;addfather&quot;) == &quot;Add&quot;:
              add_father(form)
  
              return redirect(url_for(&quot;show_family&quot;, parentsid=session[&quot;partners.id&quot;]))
  
          if request.form.get(&quot;addmother&quot;) == &quot;Add&quot;:
              add_mother(form)
  
              return redirect(url_for(&quot;show_family&quot;, parentsid=session[&quot;partners.id&quot;]))
  
          if request.form.get(&quot;addchild&quot;) == &quot;Add&quot;:
              child_forenames = form.child_forenames.data
              child_surname = form.child_surname.data
              child_fullname = fullname(child_forenames, child_surname)
  
              new_child = Individual(child_surname, child_fullname, child_forenames)
              db.session.add(new_child)
  
              db.session.commit()
              db.session.flush()
  
              session[&quot;child.id&quot;] = new_child.id
  
              link_child(individual_id=session[&quot;child.id&quot;], parents_id=session[&quot;partners.id&quot;])
  
              childlist = db.session.query(Individual.fullname).join(FamilyLink).filter(Parents.id == parentsid).all()
              children = [c[0] for c in childlist]
  
              session[&quot;children&quot;] = children
  
              form.child_forenames.data = &quot;&quot;
  
              return render_template(&quot;home.html&quot;, form=form, father_fullname=father_fullname,
                                     mother_fullname=mother_fullname, children=children) # This is probably the problem!
  
              # return redirect(url_for(&quot;show_family&quot;, parentsid=session[&quot;partners.id&quot;]))  This is the other line I've tried (see below)
  
      return render_template(&quot;home.html&quot;, form=form, father_fullname=father_fullname, mother_fullname=mother_fullname)
  </code></pre>
  <p>The relevant part of the HTML template is:</p>
  <pre><code>            &lt;form method=&quot;POST&quot; class=&quot;col-md-12&quot;&gt;
                  {{form.hidden_tag()}}
                      &lt;div class=&quot;row&quot;&gt;
                          &lt;div class=&quot;col col-sm-6&quot;&gt;
                              &lt;h4&gt;Children&lt;/h4&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;row&quot;&gt;
                          &lt;div class=&quot;col col-sm-6&quot;&gt;
                              {% if children %}
                              &lt;ul class=&quot;list-group&quot;&gt;
                                  {% for child in children %}
                                  &lt;li class=&quot;list-group-item&quot;&gt;&lt;h3&gt;{{ child }}&lt;/h3&gt;&lt;/li&gt;
                                  {% endfor %}
                              &lt;/ul&gt;
                              &lt;br&gt;
                              {% endif %}
                              &lt;div class=&quot;input-group&quot;&gt;
                                  {{ form.child_forenames() }}
                                  {{ form.child_surname() }}
                                  &lt;input type=&quot;submit&quot; class=&quot;btn btn-primary&quot; value=&quot;Add&quot; name=&quot;addchild&quot;/&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
              &lt;/form&gt;
  </code></pre>
  <p>From other answers, I'm sure the answer is to use redirect rather than render_template and potentially something around using GET (as well as the POST I need to submit the data from the form).</p>
  <p>If I use <code>return redirect(url_for(&quot;show_family&quot;, parentsid=session[&quot;partners.id&quot;]))</code> instead of  <code>render_template</code> , the form doesn't resubmit anything which is great, but it doesn't show the children list at all (even though it saves the data fine).</p>
  <p>Can anyone help please?</p>
  <p>Thank you.</p>