案件更新日期: 2016-02-10 @ 19:45 UTC
在阅读了几本与StackOverflow相关的书之后,我已经研究了几个星期了,我一直在测试和尝试不同的配置选项,使用几个代码,最后我不得不写求救,因为我被完全困住了,我一个人不能继续下去。那件事真对不起。
问题:我无法在我的应用程序中正确注册蓝图。,我做错了什么,或者做错了什么事情。属于蓝图的路由不起作用。
收到的错误:未找到。在服务器上找不到请求的URL。如果您手动输入URL,请检查您的拼写,然后重试。
我正在尝试用烧瓶和烧瓶-Restful构建API。我是刚接触过酒瓶,也是刚接触到酒瓶-Restful的,所以,你可以想象我现在的实际学习曲线有多大。
目前我的项目有以下几个方面:
/
├── PROJECT
│ ├── __init__.py
│ └── api_1_0
│ ├── __init__.py
│ ├── resources
│ │ ├── __init__.py
│ │ └── users.py
│ └── routes.py
├── config.py
└── run.py,,该项目(称为内部SelfCare)文件夹在未来将有两个或三个包在里面(对于属于同一个项目的web模块)。到目前为止,他们中唯一的一个是api_1_0。
的想法#1是构建一个具有一些基本功能的API。现在,重点是显示简单的消息(返回“hi hi”),以确认所有块都在一起工作。
的想法#2是将API构建为SelfCare包中的蓝图。现在我只能有一个API,但是以后我可以并行运行两个API (v1和v2)。
几天前,我成功地完成了idea 1,但idea 2是我无法完成的工作。抱歉的。
我当前的文件包含以下内容:
档案:run.py
from SelfCare import create_app
app = create_app(config_name='development')
if __name__ == '__main__':
app.run()档案:config.py
import os
class Config:
SECRET_KEY = 'whatever'
APP_DIR = os.path.abspath(os.path.realpath(__file__))
STATIC_DIR = os.path.join(APP_DIR, 'static')
IMAGES_DIR = os.path.join(STATIC_DIR, 'images')
STYLES_DIR = os.path.join(STATIC_DIR, 'styles')
TEMPLATES_DIR = os.path.join(STATIC_DIR, 'templates')
# Method with an application instance (for now, just a bypass)
@staticmethod
def init_app(app):
pass
class DevelopmentConfig(Config):
DEBUG = True
class TestingConfig(Config):
DEBUG = True
class ProductionConfig(Config):
DEBUG = False
# Finally, this is a dead simple configuration dictionary
config = {
# Possible profiles to run under
'development': DevelopmentConfig,
'testing': TestingConfig,
'production': ProductionConfig,
# Default application profile
'default': DevelopmentConfig
}文件:PROJECT/init.py
from flask import Flask, render_template
from flask.ext.moment import Moment
from config import config
moment = Moment()
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
# attach routes and custom error pages here
@app.route('/')
def index():
return 'Here we will start'
# HERE IT IS SUPPOSED THE BLUEPRINT IS REGISTERED
# AND ROUTES FROM BLUEPRINT ARE INCORPORATED TO MAIN PROGRAM
from .api_1_0 import api as api_blueprint
app.register_blueprint(api_blueprint)
return app文件: PROJECT/api_1_0/__init__.py
from flask import Blueprint
api = Blueprint('api', __name__)
from .resources import users文件: PROJECT/api_1_0/routes.py
from .api_1_0 import resources
@resources.route('/user', methods=['GET'])
def users():
return 'routes.py en blueprint api_1_0'文件: PROJECT/api_1_0/resources/__init__.py
This file is empty (a constructor).文件: PROJECT/api_1_0/resources/user.py
from flask_restful import Resource, reqparse, abort
# With the following dictionrary I do simulate a database connection
USERS = {
'user1': {'task': 'Build an API'},
'user2': {'task': 'Coordinate development'},
'user3': {'task': 'Build core'},
}
def abort_if_user_doesnt_exist(user):
if user not in USERS:
abort(404, message="User {} doesn't exist".format(user))
parser = reqparse.RequestParser()
parser.add_argument('task')
class User(Resource):
def get(self, user):
abort_if_user_doesnt_exist(user)
return USERS[user]
def delete(self, user):
abort_if_user_doesnt_exist(user)
del USERS[callsign]
return '', 204
def put(self, user):
args = parser.parse_args()
task = {'task': args['task']}
USERS[user] = task
return task, 201因此,routes.py似乎不起作用,所以我不能在API中更进一步。我被卡住了抱歉..。也许错误就在我身上,就像一块大石头,我看不见它。抱歉的。
任何帮助都将是非常感谢的。致以问候。
发布于 2016-02-11 16:20:45
你没有注册瓶-restful.Api. and和其他路线正确。
安装水瓶-DebugToolbar-它将帮助您显示您的注册路线。
更改代码如下:
在config.py文件中,向Config类添加以下两行:
DEBUG_TOOLBAR_ENABLED = True
REST_URL_PREFIX = '/api'在文件中:PROJECT/init.py更改为:
from flask import Flask, render_template
from flask.ext.moment import Moment
from config import config
moment = Moment()
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
try:
from flask_debugtoolbar import DebugToolbarExtension
DebugToolbarExtension(app)
except:
pass
# attach routes and custom error pages here
@app.route('/')
def index():
return render_template('index.html')
# HERE IT IS SUPPOSED THE BLUEPRINT IS REGISTERED
# AND ROUTES FROM BLUEPRINT ARE INCORPORATED TO MAIN PROGRAM
from PROJECT.api_1_0 import api_bp, API_VERSION_V1
app.register_blueprint(
api_bp,
url_prefix='{prefix}/v{version}'.format(prefix=app.config['REST_URL_PREFIX'], version=API_VERSION_V1),
)
# This registers the actual routes
import api_1_0.routes
return app在文件中:PROJECT/api_1_0/__init__.py更改为:
from flask import Blueprint
import flask_restful
API_VERSION_V1 = 1
api_bp = Blueprint('api', __name__)
api_v1 = flask_restful.Api(api_bp)将资源映射到路由;在文件中:PROJECT/api_1_0/routes.py更改为:
from PROJECT.api_1_0 import api_v1
from PROJECT.api_1_0.resources.user import User
api_v1.add_resource(User, '/user/<string:user>')您的路线现在将类似于:
返回:http://127.0.0.1:5000/api/v1/user/user1:
{
"task": "Build an API"
}返回:http://127.0.0.1:5000/api/v1/user/user2:
{
"task": "Coordinate development"
}注意路由是如何构建的;配置中的REST_URL_PREFIX加上版本号加上routes.py add_resource中的url。
发布于 2016-02-11 18:33:07
它起作用了!
我对蓝图的注册方式感到困惑。也许是因为我使用的是PyCharm 5 Pro,而且即使现在代码运行得很有魅力,IDE在文件项目/api_1_0/_init_。
api_v1 = flask_restful.Api(api_bp)api_bp 检查检测函数调用表达式中的类型错误。由于动态分派和鸭类型,这在有限但有用的情况下是可行的。函数参数的类型可以在docstring或Python 3函子注释中指定。
不,我知道我不需要太在意这个消息(或者我的IDE配置错误)。非常感谢大家。
致以问候。
https://stackoverflow.com/questions/35305458
复制相似问题