即使路线明确定义,烧瓶也会返回404

时间:2020-02-29 17:31:48

标签: flask

我有一些使用蓝图的功能:

@election_blueprint.route("/getlast/{string:type}")
def get_specific_last(election_type: str):
    some code here

然后在启动应用程序之前注册此蓝图:

app.register_blueprint(election_blueprint, url_prefix="/election")

然后,Flask说此方法是在路线中定义的:

# FLASK_APP='owo/app.py' flask routes
Endpoint                     Methods  Rule
---------------------------  -------  ----------------------------------------------------------
elections.get_elections      GET      /election/find/{string: type}
elections.get_last           GET      /election/getlast/
elections.get_specific_last  GET      /election/getlast/{string:type} <-- There it is!

但是当我尝试从客户端获取它时,我得到了404,即使其他方法(即使在此蓝图中声明)也可以正常工作。我在做什么错了?

例如,如果我刚去

http://localhost/election/getlast/sometype

它返回404,但是如果我使用其他方法,例如

http://localhost/election/getlast/

它工作正常。

1 个答案:

答案 0 :(得分:1)

我相信使用“ {”而不是“ <”可以在您的代码上书写错误。另外,路由上的变量名称应与函数上的变量名称匹配:

@election_blueprint.route("/getlast/<string:election_type>")
def get_specific_last(election_type: str):
    some code here

好的参考:https://hackersandslackers.com/flask-routes/ 希望它很适合您:)