Django messaging framework messages not making it passed the redirect() -
i have "save" code sets success message using django messaging framework upon successful creation of record, redirect main 'application_update' view. i'm having trouble determining why these messages not making thru redirect. messaging seems work fine when doing "render_to_response", not when doing "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 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 message & sets 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 piece of code:
{% if messages %} {% message in messages %} {% comment %}force -danger if error type 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 appreciated.
we use function similar 1 below preserve messages after 1 or more redirects:
# preserve error messages... ie. if redirect results in # redirect. django.contrib import messages def preserve_error_messages( request ): mstore = messages.get_messages( request ) m in mstore: messages.add_message( request, m.level, m.message, extra_tags = m.extra_tags ) return
edit: equivalent preventing messages being cleared/expired: https://docs.djangoproject.com/en/1.4/ref/contrib/messages/#expiration-of-messages
Comments
Post a Comment