External Exam Download Resources Web Applications Games Recycle Bin

search

search.py

from flask import *
app = Flask(__name__)

items = ["a1", "b2", "a3"]

@app.route("/")
def home():
  return render_template("search.html")

@app.route("/search", methods=["post"])
def search():
  term = request.form["search_term"]
  global items
  result = []
  for i in items:
    if term in i:
      result.append(i)
  return render_template("search.html", result=result)

app.run(debug=True)

templates\search.html

<form method=post action='/search'>
  <input type=text name=search_term>
  <input type=submit>
</form><br>
{% if result|length > 0 %} {{result}} {% endif %}