使用烧瓶时出现404找不到错误 [英] 404 Not found error while using flask

查看:65
本文介绍了使用烧瓶时出现404找不到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm learning web development in python with the flask. The code doesn't show any error, but when I run it, the browser shows Error - 404 Not Found. Code:

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.如果您手动输入网址,请检查拼写,然后重试

Output in Chrome: Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again

推荐答案

来自

要记住的一个重要细节是Flask如何处理斜杠.想法是使每个URL保持唯一,因此适用以下规则:

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:

  • 如果规则以斜杠结尾并且被用户要求不带斜杠,则用户将自动重定向到同一页面并附加一个斜杠.
  • 如果规则不以斜杠结尾,并且用户请求页面以斜杠结尾,则会引发未找到404的情况

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

IMO, you may encounter the second situation.

这篇关于使用烧瓶时出现404找不到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆