Skip to main content
 首页 » 编程设计

python之Flask 应用程序中的路由

2024年10月01日10yxwkf

我的 Flask 应用程序遇到路由问题。

路由定义如下

@app.route("/<lang>/books/<name>.html") 
def func1(lang="en", name="") 
    pass 
 
@app.route("/<lang>/books/index.html") 
def func2(lang="en"): 
    pass 

因此,如果请求 url,如/en/books/index.html - 它应该路由到第二个函数,但 flask 将它路由到第一个函数。

为什么会这样??我也更改了代码顺序,将上面的 func2 放在 func1 上,但仍然面临同样的问题,请问如何解决。

请您参考如下方法:

这个怎么样?

@app.route("/<lang>/books/<name>.html") 
def func1(lang="en", name=""): 
 
   if name == "index": 
        return index() 
 
   return something_else()