pebox11
pebox11

Reputation: 3748

Include or not my messages in HttpResponseRedirect?

So far (1) and (2) are producing the same results in my Development Django environment. But are they really equivalent, or I will end up loosing some messages in a setting I can not predict in advance?

(1)

return HttpResponseRedirect('/my/home/page', messages.add_message(request, messages.INFO, 'My message here'))

(2)

messages.add_message(request, messages.INFO, 'My message here')
return HttpResponseRedirect('/my/home/page')

Upvotes: 0

Views: 65

Answers (1)

Alex Morozov
Alex Morozov

Reputation: 5993

Both ways work because messages.add_message() adds the message to your request.session in-place. But the first looks very strange. The add_message doesn't return anything helpful to include in the HttpResponseRedirect. So I believe you should go with the second option:

messages.add_message(request, messages.INFO, 'My message here')
return HttpResponseRedirect('/my/home/page')

or, using a couple of shortcuts,

from django.shortcuts import redirect

messages.info(request, 'My message here')
return redirect('/my/home/page')

Upvotes: 2

Related Questions