flask将变量从一个函数传递到另一函数 [英] flask pass variable from one function to another function

查看:70
本文介绍了flask将变量从一个函数传递到另一函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以看到代码.我想将变量 q 从函数 home()传递给函数 search().

As you can see the code. I want to pass variable q from function home() into function 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)

推荐答案

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

现在您将在url中看到/search.html?q=top ,现在您可以使用 q = request.args轻松传递此 q = top .get("q") ...

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")...

@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()

这篇关于flask将变量从一个函数传递到另一函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆