【问题标题】:How to print html form details on the same page after submit in flask?在烧瓶中提交后如何在同一页面上打印html表单详细信息?
【发布时间】:2020-08-19 19:29:11
【问题描述】:

单击提交时显示没有选择值的空表单。即使单击提交后,如何在同一页面上保留图像和选择选项与选项列表。我已经提供了尽可能多的细节。提前致谢!!

这是dashboard.py

@app.route('/',methods=["GET","POST"])
def home():
     if request.method=="GET":
          #tl contains a list of topic list
          return render_template("main.html",topics=tl)
     else:
          #images_list contains a list of images
          return render_template("dashboard.html",img=images_list)

这是 main.html

<html>
<body>
<div class="header">

  <img src="static\twitter1.png" alt="logo" />
  <h1>Twitter Dashboard</h1>

</div>

<form action = "/" method = 'POST'>
    <div class="selectdiv">
<b>Topic</b>
<select name="topic_list">

{% for each in topics %}

<option value="{{each}}" selected="{{each}}">{{each}}</option>

{% endfor %}

</select>


<input type="submit" value="Submit"/>
        </div>
</form>
</body>

    {% block content %}
    {% endblock content %}

</html>

这是dashboard.html

{% extends "main.html"%}

{% block content %}

<body>

{% for row in img1 %}
<img src="static\images\{{row}}" >
{%endfor%}

</body>

{%endblock%}

结果:-

【问题讨论】:

    标签: python html flask template-inheritance


    【解决方案1】:

    您没有将选择选项传递给仪表板页面。

    @app.route('/',methods=["GET","POST"])
    def home():
        if request.method=="GET":
           #tl contains a list of topic list
           return render_template("main.html",topics=tl)
        else:
          #images_list contains a list of images
          return render_template("dashboard.html",topics=tl,img=images_list)
    

    【讨论】:

    • 请求方式为POST时,是否需要重新编写主题列表的所有代码?因为它说 tl 没有在 else 部分定义。@Navaneetha Krishnan
    • 我认为主题是一个全局变量。是的,您必须再次生成主题。或者你可以在你的 HTML 页面中使用 AJAX。
    • 好的。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 2023-04-01
    相关资源
    最近更新 更多