How to generally handle exceptions in function based and class based views?

125 views
Skip to first unread message

Daniel Grace

unread,
Nov 2, 2014, 5:18:11 PM11/2/14
to django...@googlegroups.com
Hi, I'm looking for some information / examples of how to generally handle exceptions and show a message to the user, in function based and class based views.

Aliane Abdelouahab

unread,
Nov 2, 2014, 10:24:07 PM11/2/14
to django...@googlegroups.com
why not using the classic try except ? since it is a normal python?

Daniel Grace

unread,
Nov 3, 2014, 2:03:55 PM11/3/14
to django...@googlegroups.com
OK, say if I have a function based view then how do I handle the exception?

try:
    context = RequestContext(request)
    context_dict = {}
    ...
    return render_to_response('my_file.html', context_dict, context)
except Exception as e:
    # what to do here?

What about class based views (in form_valid etc)?

Steven Cummings

unread,
Nov 3, 2014, 2:18:04 PM11/3/14
to django...@googlegroups.com
It sounded like you wanted to show something to the user, but don't keep the info from yourself as well (with respect to monitoring the site). So use the django messages framework to tell them something when wrong (not the exception details), or if you can't even render the attempted workflow then render a custom error page with the same message. Meanwhile, make sure to log the event with python logging so you can see what *really* went wrong.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a061d856-a2dd-4292-9080-693e59737e61%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Grace

unread,
Nov 3, 2014, 6:59:33 PM11/3/14
to django...@googlegroups.com
Thanks for the information Aliane and Steven, although I still don't know what to do when handling exceptions in say form_valid of a CBV.  How to render the error in this case?

Aliane Abdelouahab

unread,
Nov 3, 2014, 7:51:59 PM11/3/14
to django...@googlegroups.com
you can make for example a custom template that you call inside the except.
Reply all
Reply to author
Forward
0 new messages