On Wed, Nov 24, 2010 at 1:11 PM, Jacob Kaplan-Moss <[email protected]> wrote:
>> In my
>> case I was creating a small framework for registering functions that
>> would check for and set messages via the messages framework. If I
>> called the view and passed the request and response to these
>> functions, the messages they'd set wouldn't show until the next
>> response because the page would already have been rendered. If I could
>> stick to only passing in a request then I could put off getting the
>> response object until later, my functions could set cookies, and the
>> messages would show up at the right time.
>
> I'm really not following this "next response because the page has
> already been rendered" thing -- if you create the reponse object, pass
> it into your utility functions, then return it that creates the
> cookies just fine.

I gather that the OP is trying to do something like this:


def some_view(request):
    f(request)
    return some_response(request)

def f(request, response):
    response.COOKIES.set(...)
    messages.add_message(request, messages.INFO, "Cookie set")


The problem then is that function f needs the response object in order
to set the cookie, but if the response object is passed into f, then
it is too late to add the message.  This makes it difficult for the
same function to both set a cookie and add a message, unless it is
also responsible for creating the response.

An alternate solution might be to use delayed execution to set the cookie:

def some_view(request):
    closure = f(request)
    response = some_response(request)
    closure(response)
    return response

def f(request):
    messages.add_message(request, messages.INFO, "Cookie set")
    def closure(response):
        response.COOKIES.set(...)
   return closure

Although this could cause confusion if the message gets added, and
then the view fails in some way before the cookie actually gets set.

Cheers,
Ian

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to