pythonhtmlflasktemplate-inheritance

How to print html form details on the same page after submit in flask?


The empty form without select values is displayed when I click submit. How to retain both image and select option with option list on the same page even after clicking submit. I have provided as much details as I could. Thanks in advance!!

This is 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)

This is 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>

This is dashboard.html

{% extends "main.html"%}

{% block content %}

<body>

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

</body>

{%endblock%}

Result:- enter image description here


Solution

  • You didn't pass the select option to the dashboard page.

    @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)