views:

22

answers:

1

I'd like to call a function in my view or any module for that matter and have it update the response body.

My initial thinking is to implement a process_response middleware to update the response body, and set up a callback that receives signals sent in my function calls, but when I try, the receiver never fires (I've tested the singal/receiver outside of the middleware class/module and it works fine.

Example:

# in module that defines the signal
module.signal.send(msg='this is a message to append on the response body')

# in view or model
signal.connect(callback)

# in middleware.py
def callback(self, sender, *kwargs):
    self.body_text = kwargs.pop('msg')

def process_response(self, request, response):
    response.body = response.body + self.body_text
    return response
A: 

If you really wanted to do this you could use request as the go-between.

Use the request started/finished signals to register/deregister a new listener which will add this text to some attribute of your request object.

Then in your process_response middleware, you just have to check for that attribute. You'd also want to be careful about the response status though. There's not really any point in putting anything in the response body if it's a redirect, for example.

SmileyChris
That's not a bad solution. Right now I am just using global vars in my custom module which feels jenky, but it's mainly for development and debugging purposes so I am not too worried about that.
Derek Reynolds