Python 无法解释的 Flask 404 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24437248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:36:31  来源:igfitidea点击:

Unexplainable Flask 404 errors

pythonflask

提问by Jasmijn

I have a website that uses Flask. It used to work well, but since recently, every request returns a 404, and it seems it can't find the right endpoints. However:

我有一个使用 Flask 的网站。它曾经运行良好,但从最近开始,每个请求都返回 404,并且似乎找不到正确的端点。然而:

  • Locally, the site still works, only on my VPS it shows this strange behaviour.
  • url_forworks and app.view_functionscontains all the routes as well.
  • And yet, I keep getting 404s on the VPS, even for /and anything under /static/.
  • 在本地,该站点仍然有效,只有在我的 VPS 上它才显示出这种奇怪的行为。
  • url_for有效并app.view_functions包含所有路线。
  • 然而,我一直在VPS得到404,即使是/下什么/static/

Here's part of the code, it's a bit much to show all of it and it's not all relevant:

这是代码的一部分,显示所有内容有点多,并不是全部相关:

#snip

from flask import Flask, render_template, abort, request, redirect, url_for, session
from flask.ext.babelex import Babel
from flask.ext import babelex

#snip

app = Flask(__name__)
app.secret_key = #snip

#snip

#just one of the routes
@app.route('/')
def about():
    return render_template('about.html')

#snip

@app.errorhandler(404)
def page_not_found(e):
    #snip
    return render_template('404.html'), 404

#snip

if __name__ == '__main__':
    app.run(debug=True)
else:
    app.config.update(
        SERVER_NAME='snip.snip.com:80',
        APPLICATION_ROOT='/',
    )

回答by Scott

I know this is old, but I just ran into this same problem. Yours could be any number of issues but mine was that I had commented out the from app import viewsline in my __init__.pyfile. It gave me the same symptom: every endpoint that required @app.routeand a view was responding with 404's. Static routes (.js and .css) were fine (that was my clue).

我知道这很旧,但我遇到了同样的问题。您的问题可能有很多,但我的问题是我已经注释掉了文件中的这一from app import views__init__.py。它给了我同样的症状:需要的每个端点@app.route和视图都以 404 响应。静态路由(.js 和 .css)很好(这是我的线索)。

回答by Alexis Benoist

I had the same issue. I had it because I changed the parameters SERVER_NAMEof the config to a name which is not the name of the server.

我遇到过同样的问题。我有它是因为我SERVER_NAME将配置的参数更改为不是服务器名称的名称。

You can solve this bug by removing SERVER_NAMEfrom the config if you have it.

如果有,您可以通过SERVER_NAME从配置中删除来解决此错误。

回答by user7804097

Extremely old by this point, but mine was related to bug in importing. I'm using blueprints and I had:

在这一点上非常老,但我的与导入错误有关。我正在使用蓝图,我有:

from app.auth import bp

instead of

代替

from app.main import bp

I was importing the wrong bp/ routes

我导入了错误的 bp/ 路由

回答by Braden Holt

I received this error from defining my flask_restful route inside the create_app method. I still don't quite understand why it didn't work but once I changed the scope / moved it outside as shown below it worked.

我在 create_app 方法中定义我的flask_restful 路由时收到此错误。我仍然不太明白为什么它不起作用,但是一旦我更改了范围/将其移到外面,如下所示,它就起作用了。

from flask import Flask
from flask_restful import Resource
from extensions import db, api
from users.resources import User

def create_app():
    app = Flask(__name__)
    app.config.from_object('settings')
    db.init_app(app)
    api.init_app(app)

    return app

api.add_resource(User, '/users') 

回答by Alveona

Had the same issue because the following lines

有同样的问题,因为以下几行

if __name__ == '__main__':
    app.run(host='127.0.0.1', threaded=True)

were declared before the function and decorator.

在函数和装饰器之前声明。

Moving these lines in the very bottom of file helped me.

在文件的最底部移动这些行对我有帮助。