2017-07-29 68 views
-1

我试图让瓶转换器和运行如图所示手册中:http://flask.pocoo.org/docs/0.12/api/#url-route-registrationsPython烧瓶:转换器如何工作? (动态路由)

现在,我目前像这样定义的四页:

@app.route('/page1') 
def page1(): 
    return render_template("page1.html") 

@app.route('/page2') 
def page2(): 
    return render_template("page2.html") 

@app.route('/page3') 
def page3(): 
    return render_template("page3.html") 

@app.route('/page4') 
def page4(): 
    return render_template("page4.html") 

我找不到而是一种自动化这种重复记法的方法,并且会对提示感激不尽。

回答

2

You can create dynamic routes by using converters

你会再定义您的路线是这样的:

@app.route("/<page>") 
def pages(page): 
    return render_template(page + ".html") 

将接受所有的路径。

你也可以在你允许在动态路径,如更具体:

@app.route("/post/<int:post_id>") 
def show_post(post_id): 
    pass 

这将接受/post/1/post/2/post/100但不/post/test