title: How to pass information from html(jinja) to python(flask) through a form
body: <p>I'm trying to add the possibility to comment posts (using a new post as a comment) in the blog of The Flask Mega-Tutorial. My problem is not database related. I don't know how to "link" the form to the parent post, in a way that then the comments have reference to their parents.</p>
  
  <p>Here's a bit of code...</p>
  
  <p>The view:</p>
  
  <pre><code>@app.route('/', methods=['GET', 'POST'])
  @app.route('/index', methods=['GET', 'POST'])
  @app.route('/index/&lt;int:page&gt;', methods=['GET', 'POST'])
  @login_required
  def index(page=1):
      form = PostForm()
      if form.validate_on_submit():
          post = Post(body=form.post.data, timestamp=datetime.utcnow(),author=g.user)
          db.session.add(post)
          db.session.commit()
          flash('Your post is now live!')
          return redirect(url_for('index'))
      posts = g.user.followed_posts().paginate(page, POSTS_PER_PAGE, False)
      return render_template('index.html',
                             title='Home',
                             form=form,
                             posts=posts)
  </code></pre>
  
  <p>The form: </p>
  
  <pre><code>class PostForm(Form):
      post = StringField('post', validators=[DataRequired()])
  </code></pre>
  
  <p>The portion of the index.html (post and form in the same line...)</p>
  
  <pre><code>{% for post in posts.items %}
  &lt;table&gt;
      &lt;tr valign="top"&gt;
          &lt;td&gt;&lt;img src="{{ post.author.avatar(50) }}"&gt;&lt;/td&gt;&lt;td&gt;&lt;p&gt;&lt;a href="{{ url_for('user', nickname=post.author.nickname)}}"&gt;{{ post.author.nickname }}&lt;/a&gt; said {{ momentjs(post.timestamp).fromNow() }}:&lt;/p&gt;
  &lt;p&gt;&lt;strong&gt;{{ post.body }}&lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;
         &lt;/tr&gt;
         &lt;tr&gt;
           &lt;form action="" method="post" name="post"&gt;
           {{form.hidden_tag() }}
           &lt;table&gt;
             &lt;tr&gt;
               &lt;td&gt; &lt;p&gt;------------- &lt;/p&gt; &lt;/td&gt;
               &lt;td&gt;{{ form.post(size=10, maxlength=140)}}&lt;/td&gt;
               &lt;td&gt;
               {% for error in form.post.errors %}
               &lt;span&gt; style="color: red;"[{{error}}] &lt;/span&gt;&lt;br&gt;
               {% endfor %}
               &lt;/td&gt;
               &lt;td&gt;&lt;input type="submit" value="Reply!"&gt;&lt;/td&gt;
           &lt;/table&gt;
           &lt;/form&gt;
        &lt;/tr&gt;
  &lt;/table&gt;
  {% endfor %}
  </code></pre>
  
  <p>And the database model for Post:</p>
  
  <pre><code>class Post(db.Model):
      __searchable__ = ['body']
  
      id = db.Column(db.Integer, primary_key = True)
      body = db.Column(db.String(140))
      timestamp = db.Column(db.DateTime)
      user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
  
      parent_id = db.Column(db.Integer, db.ForeignKey('post.id'))
      children = db.relationship("Post")
  
      def __repr__(self):
          return '&lt;Post %r&gt;' % (self.body)
  </code></pre>
  
  <p>My idea was to add a new post as a "comment", giving to it a database reference to the old post (parent_id). How can I configure the form so it knows who is the parent (the parent_id)? Should I work on the index.html and find a way to give to the form the information about the parent post? </p>
  
  <p>Thank you very much! </p>