python - flask 蓝图 404

标签 python flask

我在尝试访问 Flask 蓝图中定义的路由时收到 404,但我不明白为什么。有没有人看到我做错了什么(一般来说我是 Flask 和 Python 的新手,所以它可能是一些基本的东西)?

我的蓝图(test.py):

from flask import Blueprint, jsonify, request
from werkzeug.local import LocalProxy

test_blueprint = Blueprint(
    'test_blueprint', __name__, url_prefix='/test')


@test_blueprint.route('/test', methods=['GET'])
def get_all_tests():

    return jsonify([{
        "id": "1",
        "name": "Foo"
    }, {
        "id": "2",
        "name": "Bar"
    }, {
        "id": "3",
        "name": "Baz"
    }]), 200

我的app.py:

from test import test_blueprint

from flask import abort, Flask
from os import environ


def create_app():

    app = Flask(__name__)

    app.config.from_object('config.settings')
    app.template_folder = app.config.get('TEMPLATE_FOLDER', 'templates')
    app.url_map.strict_slashes = False

    app.register_blueprint(test_blueprint)

    return app

点击http://127.0.0.1:5000/test的结果是:

(venv) mymachine:api myusername$ python run.py
 * Restarting with stat
Starting server at 127.0.0.1 listening on port 5000 in DEBUG mode
 * Debugger is active!
 * Debugger PIN: 123-456-789
127.0.0.1 - - [2018-06-03 17:02:56] "GET /test HTTP/1.1" 404 342 0.012197

app.pytest.py 在同一个目录下,仅供引用。


额外信息: 因为你可以在上面看到我从一个名为 run.py 的文件开始,为了完整起见,这里是该文件:

from flask_failsafe import failsafe
from gevent import monkey
from gevent.pywsgi import WSGIServer
from werkzeug.debug import DebuggedApplication
from werkzeug.serving import run_with_reloader

@failsafe
def failsafe_app():
    from app import create_app
    return create_app()


app = failsafe_app()


@run_with_reloader
def run():

    app.debug = app.config['DEBUG']

    print('Starting server at %s listening on port %s %s' %
          (app.config['HOST'], app.config['PORT'], 'in DEBUG mode'
           if app.config['DEBUG'] else ''))

    if app.config['DEBUG']:
        http_server = WSGIServer((app.config['HOST'], app.config['PORT']),
                                 DebuggedApplication(app))
    else:
        if app.config['REQUEST_LOGGING']:
            http_server = WSGIServer((app.config['HOST'], app.config['PORT']),
                                     app)
        else:
            http_server = WSGIServer(
                (app.config['HOST'], app.config['PORT']), app, log=None)

    http_server.serve_forever()

最佳答案

当您使用 url_prefix 定义蓝图时,此蓝图的每条规则都会将此前缀与给定的路由连接起来

在您的示例中,url 应该是 http://127.0.0.1:5000/test/test 以访问 View get_all_tests

关于python - flask 蓝图 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50670996/

相关文章:

python - Numpy 在索引周围创建掩码数组的最快方法

python - 如何使用exec()执行Python程序并获取变量?

python - 每个掩码分别选择和乘以 numpy 二维元素(无循环)

json - 使用 test_request_context (Flask) 中传递的无效 JSON 进行测试

python - sqlalchemy线程消息系统

python - 如何找到总和最多为一个常数的所有组合?

python - 用一根不同的线绘制数据框

python - Flask - 在应用程序上下文之外工作

python - 使用 Flask : ImportError: No module named bcrypt 运行 python 服务器时出错

python - 有没有办法使用 Python 的 Celery 在具有特定任务 ID 的特定任务成功或失败时立即收到通知?