redirect while passing arguments

后端 未结 4 795
情深已故
情深已故 2020-11-27 15:07

In flask, I can do this:

render_template(\"foo.html\", messages={\'main\':\'hello\'})

And if foo.html contains {{ messages[\'main\']

4条回答
  •  情书的邮戳
    2020-11-27 15:49

    I'm a little confused. "foo.html" is just the name of your template. There's no inherent relationship between the route name "foo" and the template name "foo.html".

    To achieve the goal of not rewriting logic code for two different routes, I would just define a function and call that for both routes. I wouldn't use redirect because that actually redirects the client/browser which requires them to load two pages instead of one just to save you some coding time - which seems mean :-P

    So maybe:

    def super_cool_logic():
        # execute common code here
    
    @app.route("/foo")
    def do_foo():
        # do some logic here
        super_cool_logic()
        return render_template("foo.html")
    
    @app.route("/baz")
    def do_baz():
        if some_condition:
            return render_template("baz.html")
        else:
            super_cool_logic()
            return render_template("foo.html", messages={"main":"Condition failed on page baz"})
    

    I feel like I'm missing something though and there's a better way to achieve what you're trying to do (I'm not really sure what you're trying to do)

提交回复
热议问题