flask - 即使明确定义了路线,Flask 也会返回 404
问题描述
我有一些使用蓝图的功能:
@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/
它工作正常。
解决方案
我相信使用“{”而不是“<”在您的代码上写入的方式存在错字。此外,路由上的变量名应与函数上的变量名匹配:
@election_blueprint.route("/getlast/<string:election_type>")
def get_specific_last(election_type: str):
some code here
很好的参考:https ://hackersandslackers.com/flask-routes/ 希望它适合你:)
推荐阅读
- java - 确定代码的哪一部分负责使程序按照用户在测试次数中所说的去做
- typescript - TypeScript 中的类型谓词
- java - 二进制到十进制转换器的输入验证
- linux - 保持应用程序运行,如果它崩溃,请重新启动它。Ubuntu 16
- android - 无法获取用户位置
- visual-studio-code - 粘贴到 VS Code 中的文本有空格但不注册所有空格
- javascript - 通过 socket.io 在服务器端存储新添加的元素
- c# - log4net:错误无法创建附加程序 [RollingLogFileAppender]
- java-8 - 使用 Zulu jdk 11 从 Alpine Linux Image 调用 GRPC 服务时出现 SSL 连接问题
- r - 如何编写中缀函数?