views:

22

answers:

1

I want to be able to send a message using the new messages framework. Something along the lines of :

...
if formset.is_valid    
    return HttpResponseRedirect( some page )   
    messages.add_message(request,messages.INFO, '%i objects added') %formset.number_of_forms

But two questions:

  1. Im not sure if i should put the messages before or after the render to response
  2. Is there a method akin to number_of_forms
A: 
  1. I don't see a render_to_response in your snippet, but not only should any calls to messages.add_message() go before any render_to_response() but it should also go before any return statement as you have in your snippet. If you put any code after a return statement it will never be executed.

  2. In python the len() function is always used to count the number of objects in a list (or any type of iterator). So len(formset.forms) should give you the number of forms in the formset.

Van Gale
after successful saving of a formset, I was using HttpResponseRedirect, but that dosent pass along context (with the message) does it? Should i use something else in the return statement?
Dave
Well, use a redirect if you actually want to redirect to another page, otherwise render your response and return that. You are correct, using a redirect will lose any of your context from the current view, but that shouldn't matter for messages which are session/cookie based ... which means they will be in the next context regardless of where that context is being generated. You just need to add the messages to session before calling redirect.
Van Gale