如何使用装饰器将两个变量传递给函数

时间:2019-09-02 19:27:05

标签: python flask decorator

我有一个函数(“邮政编码”),该函数从用户处获取输入并生成两个变量,我希望将这些变量传递给另一个函数(“选区”)。

如何在函数之间传递两个参数,以便仅将一个传递给url?

app("/postcode", methods=["GET", "POST"])
def postcode():
    form = PostcodeForm()
    if form.validate_on_submit():        
        pc = form.postcode.data.replace(" ", "").strip()
        if validate(pc) == True:
            code, const = dosomething(pc)
            return redirect(url_for('app.constituency', const_id=const)), code

    return render_template("template1.html", form=form)


@app.route("/constituency/<string:const_id>")
def constituency(const_id):
    const = const_id
    people = People.query.filter_by(code=code) # I want the value of "code" here to come from the function 'postcode' above
    return render_template("template2.html", people=people, const=const)

1 个答案:

答案 0 :(得分:0)

您要么需要传递code作为查询参数,要么创建一个带有code参数的新网址路由,url_for不会直接调用consitutency,它只是生成浏览器然后请求的网址。