Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django raising 404 with a message

I like to raise 404 with some error message at different places in the script eg: Http404("some error msg: %s" %msg) So, in my urls.py I included:

handler404 = Custom404.as_view()

Can anyone please tell me how should I be handling the error in my views. I'm fairly new to Django, so an example would help a lot.
Many thanks in advance.

like image 310
DGT Avatar asked May 03 '13 20:05

DGT


People also ask

How do I fix 404 error in Django?

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page. Let's do that by turning off Django debug mode. For this, we need to update the settings.py file.


2 Answers

Yes we can show specific exception message when raise Http404.

Pass some exception message like this

raise Http404('Any kind of message ')

Add 404.html page into templates directory.

templates/404.html

{{exception}}
like image 77
Muhammad Faizan Fareed Avatar answered Oct 21 '22 12:10

Muhammad Faizan Fareed


if you want to raise some sort of static messages for a particular view , you can do as follows:-

from django.http import Http404

def my_view(request):
  raise Http404("The link seems to be broken")
like image 42
Gagan Avatar answered Oct 21 '22 13:10

Gagan