Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask after_request 'NoneType' object is not callable [duplicate]

Tags:

python

flask

I have a pretty heavy application built on flask-restful so I will just present the smaller version of the problem here. I have divided my application into modules having the structure

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)

The problem here is after dividing the code everything works except afer_request. I need to perform some database related operations for logging in the request data after each request. When I run the main-file.py, I get TypeError: 'NoneType' object is not callable

Somehow I have understood that the response obj in the after_request is giving None. I am dead stuck here on making that after_request to work. Any help will be appreciated. Thanks.

like image 941
Lalitha Ramkumar Avatar asked Dec 07 '25 08:12

Lalitha Ramkumar


1 Answers

Return the response after you're done:

@app.after_request         #This block fails
def after(response):
    # do your database stuff
    return response
like image 186
Oin Avatar answered Dec 08 '25 22:12

Oin