Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将变量从一个函数传递到另一个函数_Python_Function_Flash Message - Fatal编程技术网

Python 将变量从一个函数传递到另一个函数

Python 将变量从一个函数传递到另一个函数,python,function,flash-message,Python,Function,Flash Message,正如您所看到的代码。 我想将变量q从函数home()传递到函数search() 选项1: 在函数中创建和更新的变量只存在于该函数中 var_list = [] @app.route("/",methods=['GET','POST']) def home(): result = Mylist.query.all() return render_template('index.html',result=result) q = request.form.g

正如您所看到的代码。 我想将变量
q
从函数
home()
传递到函数
search()


选项1:

在函数中创建和更新的变量只存在于该函数中

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)

“如果在函数体的任何位置为变量赋值, 除非明确声明为全局,否则假定它是本地的。”


解决方案:
q
创建为函数外部的变量,这样一来,它不仅被困在函数
home
中,而且现在对任何和所有函数都是通用的。任何函数都可以使用或更新这样的变量

选项2:

或者,您可以尝试将
q
作为函数参数传递

在下面的示例中,您将调用函数
search
,但添加了参数
q
search
函数本身将引用与
q
相同的
input
(或者选择您自己的名称/单词)


其中
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>

您的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>

定义一个全局列表将变量from first view函数追加到该列表,并访问第二个view函数中的最后一个索引值

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)

result=Mylist.query.filter(Mylist.type.like(var))您的
q
在返回stmt之后?这可能会对您有所帮助,我发现它比现有答案更好。它显示了一个错误,TypeError:search()缺少1个必需的位置参数:“input”关于“search()缺少1个必需的参数”听起来好像您试图调用
search()
,但没有给它搜索内容。如您所见,函数
def search(input):
要求至少提供一件东西作为
input
…我正在函数中保存一个文件名,需要将该变量/文件名传递给另一个函数。在这种情况下,你的解决方案是不可能的。
<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>
q=request.args.get("q")
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)