1

So basically, I want a middleware to connect a signal listener that adds stuff to the request object. This information can then be included in a response. I currently have this:

class SomeMiddleware:
    def process_request(self, request):
        request.signals_received = ["I can see this"]

        def listener(sender, **kwargs):
            print "I can see this too"
            request.signals_received.append("I can't see this")

        some_signal.connect(listener)

    def process_response(self, request, response):
        response.write(request.signals_received)
        return response

The problem is, I can't see stuff I append in the listener. What up with that?

Ultimately, the listener should register parallel tasks which may or may not be done in time to add them to the response just before rendering.

edit - quotes in snippet

Jesse the Game
  • 2,600
  • 16
  • 21
  • Are you saying that, in `process_response()`, `request.signals_received` is `["I can see this"]`? Do you get any exception? If you don't need to access `request.signals_received` from other points, I'd just store in an attribute of the middleware class itself – redShadow Mar 21 '12 at 17:05
  • I tested this using the django shell and I don't get any exceptions at all. I would like to be able to have access to `request.signals_received` elsewhere, but I can probably work around that with careful ordering of middleware. – Jesse the Game Mar 21 '12 at 17:16
  • This is a very interesting and unusual use case. I'm interested in learning more details about why you need this set up in this way. – Jordan Reiter Mar 21 '12 at 17:21

0 Answers0