I have some "save" code that sets a success message using the Django messaging framework upon successful creation of a record, then does a redirect back to the main 'application_update' view. I'm having trouble determining why these messages are not making it thru the redirect. The messaging seems to work just fine when doing a "render_to_response", but not when doing a "redirect".
function snippet (if POST) in views.py:
if ovrd_form.is_valid():
fields = {'application': ovrd_form.cleaned_data['application'],
'course': ovrd_form.cleaned_data['course'],
* other field/values *
}
try:
overrides = Overrides(**fields)
overrides.save()
success_msg = 'Override creation was successful.'
create_message(request, success_msg, 'success')
except Exception, exception:
return HttpResponse('Error: ' + str(exception))
return redirect('application_update', app_id=app_id)
create_message() function:
from django.contrib import messages
def create_message(request, msg, msg_type):
""" build a message & sets the correct message.type """
if msg_type == 'error':
django.contrib.messages.error(request, msg)
elif msg_type == 'warning':
django.contrib.messages.warning(request, msg)
elif msg_type == 'success':
django.contrib.messages.success(request, msg)
elif msg_type == 'info':
django.contrib.messages.info(request, msg)
elif msg_type == 'debug':
django.contrib.messages.debug(request, msg)
all templates inherit this piece of code:
{% if messages %}
{% for message in messages %}
{% comment %}force -danger if error type for bootstrap css class{% endcomment %}
{% if message.tags == 'error' %}
<div class="alert alert-danger">
{% else %}
<div class="alert alert-{{ message.tags }}">
{% endif %}
<button type="button" class="close" data-dismiss="alert">×</button>
<span>{{ message }}</span>
</div>
{% endfor %}
{% endif %}
Any help would be very much appreciated.