Django - catch exception
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: RPG Blues Looping
--
Chapters
00:00 Question
00:53 Accepted answer (Score 22)
01:24 Answer 2 (Score 4)
01:51 Answer 3 (Score 1)
02:41 Answer 4 (Score 1)
03:18 Thank you
--
Full question
https://stackoverflow.com/questions/1089...
Accepted answer links:
[404 handler or 500 handler]: https://docs.djangoproject.com/en/dev/to...
Answer 2 links:
[Django 404 error]: https://docs.djangoproject.com/en/dev/to...
[Django redirect]: https://docs.djangoproject.com/en/dev/to...
Answer 4 links:
[Django messaging framework]: https://docs.djangoproject.com/en/1.10/r.../
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #exception
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: RPG Blues Looping
--
Chapters
00:00 Question
00:53 Accepted answer (Score 22)
01:24 Answer 2 (Score 4)
01:51 Answer 3 (Score 1)
02:41 Answer 4 (Score 1)
03:18 Thank you
--
Full question
https://stackoverflow.com/questions/1089...
Accepted answer links:
[404 handler or 500 handler]: https://docs.djangoproject.com/en/dev/to...
Answer 2 links:
[Django 404 error]: https://docs.djangoproject.com/en/dev/to...
[Django redirect]: https://docs.djangoproject.com/en/dev/to...
Answer 4 links:
[Django messaging framework]: https://docs.djangoproject.com/en/1.10/r.../
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #exception
#avk47
ACCEPTED ANSWER
Score 23
You have three options here.
- Provide a 404 handler or 500 handler
- Catch the exception elsewhere in your code and do appropriate redirection
- Provide custom middleware with the
process_exceptionimplemented
Middleware Example:
class MyExceptionMiddleware(object):
def process_exception(self, request, exception):
if not isinstance(exception, SomeExceptionType):
return None
return HttpResponse('some message')
ANSWER 2
Score 4
You can raise a 404 error or simply redirect user onto your custom error page with error message
from django.http import Http404
#...
def your_view(request)
#...
try:
#... do something
except:
raise Http404
#or
return redirect('your-custom-error-view-name', error='error messsage')
ANSWER 3
Score 2
If you want to get proper traceback and message as well. Then I will suggest using a custom middleware and add it to the settings.py middleware section at the end.
The following code will process the exception only in production. You may remove the DEBUG condition if you wish.
from django.http import HttpResponse
from django.conf import settings
import traceback
class ErrorHandlerMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
return response
def process_exception(self, request, exception):
if not settings.DEBUG:
if exception:
message = "{url}\n{error}\n{tb}".format(
url=request.build_absolute_uri(),
error=repr(exception),
tb=traceback.format_exc()
)
# Do whatever with the message now
return HttpResponse("Error processing the request.", status=500)
ANSWER 4
Score 1
Another suggestion could be to use Django messaging framework to display flash messages, instead of an error page.
from django.contrib import messages
#...
def another_view(request):
#...
context = {'foo': 'bar'}
try:
#... some stuff here
except SomeException as e:
messages.add_message(request, messages.ERROR, e)
return render(request, 'appname/another_view.html', context)
And then in the view as in Django documentation:
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}