【发布时间】:2015-09-27 06:37:49
【问题描述】:
我正在使用 RealPython,但在烧瓶动态路由方面遇到了问题。
在动态路由之前,一切似乎都正常。现在,如果我尝试输入“搜索查询”(即 localhost:5000/test/hi),则找不到该页面。 localhost:5000 仍然可以正常工作。
# ---- Flask Hello World ---- #
# import the Flask class from the flask module
from flask import Flask
# create the application object
app = Flask(__name__)
# use decorators to link the function to a url
@app.route("/")
@app.route("/hello")
# define the view using a function, which returns a string
def hello_world():
return "Hello, World!"
# start the development server using the run() method
if __name__ == "__main__":
app.run()
# dynamic route
@app.route("/test/<search_query>")
def search(search_query):
return search_query
我看不到其他使用 RealPython 的人在使用相同的代码时遇到过问题,所以我不确定我做错了什么。
【问题讨论】:
-
那么你把动态路由放在
app.run()之后了吗? -
好吧,你的代码在这里可以很好地工作。
localhost:5000/test/hi、localhost:5000或localhost:5000/hello将返回正确的字符串,我没有收到 404 错误。 -
重启电脑后就可以了。