Option 1:
A variable created and updated inside a function exists only for that function.
From the Python documentation...
"If a variable is assigned a value anywhere within the function’s body,
it’s assumed to be a local unless explicitly declared as global."
Solution:
Create q
as a variable outside of functions, this way it's not just trapped inside function home
but instead is now universally available to any and all functions. Any function can use or update such a variable.
Option 2:
Alternatively, you could just try passing q
as a function parameter.
In example below, you will call function search
but with parameter q
added. The search
function itself will reference that same q
as a thing called input
(or choose your own name/word).
@app.route("/",methods=['GET','POST'])
def home():
result = Mylist.query.all()
q = request.form.get("q")
search( q )
return render_template('index.html',result=result)
@app.route("/search.html")
def search( input ):
d = input
var='%'+d+'%'
result = Mylist.query.filter(Mylist.type.like(var)
return render_template('search.html',result=result)