-1

My application will be be pulling a query from a sample list eventually. For the time being I am trying to present the term entered into the HTML submit page onto the result HTML page. At the moment "queryterm" is not showing up on my result page. I have a feeling the term is not defined on the @app.route("/result") which is why nothing is showing up. The variable is empty. How would I go about solving my issue? I am new to flask!

Python

from flask import Flask, render_template, request, url_for, redirect
from test_list import test_list

app = Flask(__name__)

@app.route("/")
def home_page():
    return render_template('index.html')

@app.route("/submit", methods=["POST", "GET"])
def submit():
    if request.method == "POST":
        queryterm = request.form["search"]
        return redirect(url_for("result", queryterm=queryterm))
    else:
        return render_template('submit.html')

@app.route("/result")
def result():
    result = test_list
    return render_template('result.html', result=result)

HTML Submit Page

{% block title %}Submit Page{% endblock %}

{% block content %}
<form action="#" method="post">
    <p>Enter a search term</p>
    <p><input type ="text" name="search" /></p>
    <p><input type="submit" value="submit" /></p>

</form>
{% endblock %}

HTML Result Page

{% block title %}Result Page{% endblock %}

{% block content %}

<p>The query for {{ queryterm }} has found:</p>
<ul>
{% for item in result %}
    <li>{{ item }}</li>
{% endfor %}
</ul>


{% endblock %}

1 Answers1

-1

As far as I understood your question you can do one thing. Create an empty list at the top of all routes. After that, you can append and access the data to that list anywhere in the program.

your_list = []
@app.route("/some_route")
'
'
your_list.append(your_data)

@app.route("/some_other_route")
'
'
print(your_list)
Shakir Sadiq
  • 64
  • 1
  • 5
  • I tried what you suggested and unfortunately the value for the new list still does not show up on the result.html page. – Jesse Caddell Mar 10 '22 at 05:51
  • Do you want to show that list's content in result.html? if yes please do not use render_template. Simply use return"your_list[index_value]". And if this solves your issue. You can debug your code further for render_template. – Shakir Sadiq Mar 10 '22 at 06:02
  • Should I just scrap the idea of having an HTML page to display the term searched for and the list contents? I was able to make it work by adding an argument for the term, but I figured I should learn how to present it through a new HTML page. – Jesse Caddell Mar 10 '22 at 06:08
  • Please check this https://stackoverflow.com/a/54848859/15090325, this might help you. – Shakir Sadiq Mar 10 '22 at 07:02