Correctly pass view errors to template with Django -
how can pass errors display them in templates, using clean , common way ?
i'd show errors through overlay box message.
- transmit error object each return ?
- push in session, , check in template ?
- check template using ajax method ?
- use common error template page ? (not user friendly..)
i'm confused when using httpresponseredirect ; unless using session variables, it's not possible pass errors.
the usual way use django's message framework, rohan said.
anywhere in view, can use code pass errors templates :
from django.contrib import messages def my_view(request): … messages.error(request, 'oops, bad happened') return redirect…
you can add following code in base.html
template display messages (here using bootstrap classes):
{% if messages %} <div class="span12"> {% message in messages %} <div class="alert alert-{{ message.tags }}"> {{ message|safe }} </div> {% endfor %} </div> {% endif %}
Comments
Post a Comment