gpt4 book ai didi

python-3.x - 使用 flask 时找不到404错误

转载 作者:行者123 更新时间:2023-12-03 08:23:25 24 4
gpt4 key购买 nike

我正在用 flask 学习python的Web开发。该代码未显示任何错误,但是当我运行它时,浏览器显示错误-找不到404。
码:

from flask import Flask, redirect, url_for
app = Flask(__name__)
@app.route('/admin/')
def hello_admin():
return 'Hello Admin'
@app.route('/guest/<guest>')
def hello_guest(guest):
return 'Hello %s as Guest' % guest
@app.route('/user/<name>')
def hello_user(name):
if name =='admin':
return redirect(url_for('hello_admin'))
else:
return redirect(url_for('hello_guest',guest = name))
if __name__ == '__main__':
app.run(debug=True)

在Chrome中输出:
未找到
在服务器上找不到请求的URL。如果您手动输入网址,请检查拼写,然后重试

最佳答案

Flask documentation:

An important detail to keep in mind is how Flask deals with trailing slashes. The idea is to keep each URL unique so the following rules apply:

  • If a rule ends with a slash and is requested without a slash by the user, the user is automatically redirected to the same page with a trailing slash attached.
  • If a rule does not end with a trailing slash and the user requests the page with a trailing slash, a 404 not found is raised

IMO,您可能会遇到第二种情况。

关于python-3.x - 使用 flask 时找不到404错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50890629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com