This exception is util when you want to raise an exception but want its message be shown as a message to the user, with no error 500 or 404 pages.
To use it, just append the middleware in the MIDDLEWARE_CLASSES setting and raises HttpMessage when necessary.
1 2 3 4 5 6 7 8 9 10 | from django.http import HttpResponseRedirect
class HttpMessageMiddleware(object):
def process_exception(self, request, exception):
if request.user.is_authenticated():
request.user.message_set.create(message=unicode(exception))
return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
class HttpMessage(Exception):
pass
|
More like this
- Stuff by NixonDash 1 month ago
- Add custom fields to the built-in Group model by jmoppel 3 months ago
- Month / Year SelectDateWidget based on django SelectDateWidget by pierreben 6 months, 2 weeks ago
- Python Django CRUD Example Tutorial by tuts_station 7 months ago
- Browser-native date input field by kytta 8 months, 2 weeks ago
Comments
It can go in infinite loop. May be some query string parameter can be used to circumvent that.
#
This recipe catches all errors. "except HttpMessage" is missing in process_exception().
#
Please login first before commenting.