Flask Restful URL在蓝图中提供404

时间:2016-07-19 11:07:23

标签: flask flask-restful

我正在尝试将Flask restful用作蓝图,其模式适用于其他蓝图。我去的时候一直收到404错误 /待办事项/ 1

我的项目设置如下:

文件夹结构

├── app
│   ├── __init__.py
│   ├── mod_api
│   │   ├── __init__.py
│   │   └── routes.py
│   ├── main
│   │   ├── __init__.py
│   │   ├── forms.py
│   │   └── views.py
│   └── templates
│       ├── base.html
│       └── home.html
├── config.py
├── manage.py
└── requirements.txt

__初始化__。PY

from flask import Flask
from flask_restful import Api
from flask_bootstrap import Bootstrap
from config import config
bootstrap = Bootstrap()
api = Api()

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    bootstrap.init_app(app)
    api.init_app(app)

    from .main import main as main_blueprint
    from .mod_api import mod_api as api_blueprint
    app.register_blueprint(main_blueprint)
    app.register_blueprint(api_blueprint)
    return app

mod_api / __初始化__。PY

from flask import Blueprint

mod_api = Blueprint('mod_api', __name__)

from . import routes

API / routes.py

from flask_restful import Resource
from .. import api

class TodoItem(Resource):
    def get(self, id):
        return {'task': 'Say "Hello, World!"'}

api.add_resource(TodoItem, '/todos/<int:id>')

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我有同样的问题,解决方案是:

api.init_app(api_blueprint)