[英]flask pass variable from one function to another function
As you can see the code.正如您所看到的代码。 I want to pass variable q
from function home()
into function search()
.我想将变量q
从函数home()
传递到函数search()
。
@app.route("/",methods=['GET','POST'])
def home():
result = Mylist.query.all()
return render_template('index.html',result=result)
q = request.form.get("q")
@app.route("/search.html")
def search():
d = q
var='%'+d+'%'
result = Mylist.query.filter(Mylist.type.like(var)
return render_template('search.html',result=result)
Where an index.html
will contain:其中index.html
将包含:
<form action="/search.html" method="get" autocomplete="off" class="subscribe-form">
<div class="form-group d-flex">
<input type="text" class="form-control" placeholder="Enter your search" name="q" id="q" value="{{q}}">
<input type="submit" value="Search" class="submit px-3">
</div>
</form>
Now you will see /search.html?q=top
in url now you can easily pass this q=top
by using q=request.args.get("q")
...现在您将在 url 中看到/search.html?q=top
现在您可以使用q=request.args.get("q")
轻松传递这个q=top
...
@app.route("/search.html",methods=['GET'])
def search():
q =request.args.get("q")
d=str(q)
var='%'+d+'%'
myresult = Mylist.query.filter(Mylist.type.like(var)| Mylist.title.like(var)).all()
Option 1:选项1:
A variable created and updated inside a function exists only for that function.在函数内部创建和更新的变量仅存在于该函数中。
From the Python documentation ...从Python 文档...
"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.创建q
作为函数之外的变量,这样它不仅被困在函数home
而且现在普遍适用于任何和所有函数。 Any function can use or update such a variable.任何函数都可以使用或更新这样的变量。
Option 2:选项 2:
Alternatively, you could just try passing q
as a function parameter .或者,您可以尝试将q
作为函数参数传递。
In example below, you will call function search
but with parameter q
added.在下面的示例中,您将调用函数search
但添加了参数q
。 The search
function itself will reference that same q
as a thing called input
(or choose your own name/word). search
功能本身将引用相同的q
作为称为input
的东西(或选择您自己的名字/单词)。
@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)
Define a global list append the variable from first view function to the list and access the last index value in the second view function.定义一个全局列表,将第一个视图函数中的变量附加到列表中,并访问第二个视图函数中的最后一个索引值。
var_list = []
@app.route("/",methods=['GET','POST'])
def home():
result = Mylist.query.all()
return render_template('index.html',result=result)
q = request.form.get("q")
var_list.append(q)
@app.route("/search.html")
def search():
d = var_list.pop()
var='%'+d+'%'
result = Mylist.query.filter(Mylist.type.like(var)
return render_template('search.html',result=result)
your index.html will contain您的 index.html 将包含
<form action="/search.html" method="get" autocomplete="off" class="subscribe-form">
<div class="form-group d-flex">
<input type="text" class="form-control" placeholder="Enter your search" name="q" id="q" value="{{q}}">
<input type="submit" value="Search" class="submit px-3">
</div>
</form>
================================================================================== ================================================== ================================
Now you will see /search.html?q=top in url now you can easily pass this q=top by using现在您将在 url 中看到 /search.html?q=top 现在您可以通过使用轻松地传递这个 q=top
q=request.args.get("q")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.