Я получаю 404 при попытке получить доступ к маршруту, определенному в «Плане Flask», и не понимаю, почему.Кто-нибудь видит, что я делаю неправильно (я новичок в 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.py
и test.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()