2015-06-19 59 views
0

我正在用python开发一个使用python的博客应用程序。在视图函数中,表单作为参数传递给render_template,它调用'index.html'。表单按预期工作在index.html中。但是有一个放置'post.htm'的{%include''%}标签。现在我想在'post.html'中重复使用相同的表单。怎么做?将传递给index.html的表单也包含在“post.html”中吗?如果是的话,如何识别呈现页面中按下哪个按钮,因为两个表单都是相同的?我的index.html文件:如何将参数传递给包含在Flask web应用程序中的html

{% extends "base.html" %} 
{% block content %} 
    <h1>Hi, {{ g.user.nickname }}!</h1> 
    <form action="" method="post" name="post"> 
     {{ form.hidden_tag() }} 
    <table> 
     .......... 
     .......... 
     <tr> 
      <td><input type="submit" value="Post!"></td> 
     </tr> 
    </table> 
    </form> 


    {% for post in posts.items %} 
    <div class="{{ post.id }}"> 
     <div> 
      {% include 'post.html' %} 
     </div> 
    </div> 
    {% endfor %} 

{% endblock %} 

而且我post.html为:

<table> 
    .......... 
    .......... 
    <tr valign="top"> 
     <td>{{ post.body }}</td> 
    </tr> 
    <tr> 
    <form action="" method="post" name="post"> 
     {{ form.hidden_tag() }} 
     <table> 
      ......... 
      ......... 
      <td><input type="submit" value="Post!"></td>     
     </table> 
    </form> 

</table> 

是否有可能使用同一表格上的两个HTML文件。由于在渲染的网页中会有几个表单域,如何识别哪个按钮被按下? index.html的呈现与代码:

return render_template('index.html', 
          title='Home', 
          form=form, 
          posts=posts) 

回答

3

为了区分不同的形式,你需要一些独特的关键,例如一个隐藏的输入标签,其中包含一个ID:

<input type="hidden" name="post_id" value="{{post.id}}"> 

对于index-Form,您可以将其用作通用标识值“new”。

相关问题