烧瓶蓝图404 [英] Flask Blueprint 404

查看:52
本文介绍了烧瓶蓝图404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试访问Flask蓝图中定义的路由时得到404,但我不明白为什么.有人看到我在做错什么吗(我是Flask和Python的新手,所以可能是基本的东西)?

I get a 404 when trying to access a route defined in a Flask Blueprint and I do not see why. Does anyone see what I am doing wrong (I'm new to Flask and Python in general so it could be something basic)?

我的蓝图( 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.py test.py 位于同一目录中.

其他信息:既然您可以在上面看到,我从一个名为 run.py 的文件开始,为了完整起见,这里是该文件:

Extra Info: Since you can see above that I am starting this with a file named run.py here is that file, for completeness:

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 定义蓝图时,该蓝图中的每个规则都会将该前缀与给定的路由连接起来

When you define the blueprint with an url_prefix, every rules of this blueprint will concatenate this prefix with the given routes

在您的示例中,该网址应为 http://127.0.0.1:5000/test/test ,以访问视图 get_all_tests .

In your example, the url should be http://127.0.0.1:5000/test/test to access the view get_all_tests.

这篇关于烧瓶蓝图404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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