我有一个非常繁重的应用程序建立在burn-restful上,所以我将在这里展示问题的较小版本。我已将应用程序划分为具有结构
的模块Folder A:
__init__.py (empty)
main-file.py (executable file)
other-file.py
other-file.py
from flask_restful import reqparse, abort, Api, Resource
from flask import Flask
TODOS = {
'todo1': {'task': 'build an API'},
'todo2': {'task': '?????'},
'todo3': {'task': 'profit!'},
}
class TodoList(Resource):
def get(self):
return TODOS
main-file.py
from flask import Flask,request
from other-file import *
app = Flask(__name__)
api = Api(app)
@app.before_request
def before_request():
print 'before request'
@app.after_request #This block fails
def after(response):
print 'after request'
#I need to perform some db operations here
api.add_resource(TodoList, '/todos')
if __name__ == '__main__':
app.run(debug=True)
这里的问题是在划分代码之后除了afer_request之外一切正常。我需要执行一些与数据库相关的操作,以便在每次请求后登录请求数据。当我运行main-file.py时,我得到TypeError: 'NoneType' object is not callable
不知怎的,我已经理解了after_request中的响应obj给出了None。我被困在这里使after_request工作。任何帮助将不胜感激。感谢。
答案 0 :(得分:7)
完成后返回响应:
@app.after_request #This block fails
def after(response):
# do your database stuff
return response