Raise Http404 in url template

I am trying to override the url in django profiles to raise 404 instead of going to view.

I am looking for something like:

url(r'^profiles/$', lamdba x: raise Http404) 

But the above does not work. Is it possible?

I know that I can write a new view that calls 404, but I would prefer if I could do this in the url template directly.

edit: In particular, I get invalid syntax (urls.py, line 29) with the above example.

+4
source share
3 answers
+10
source

Starting with Django 1.9 , the page_not_found now accepts the second parameter, the exception that caused the error. "Therefore, the solution proposed by Kristoffer requires minor changes:

 from django.views.defaults import page_not_found urlpatterns = patterns('', url(r'^profiles/$', page_not_found, {'exception': Exception('Not Found')}), ) 

Hope this helps.

+6
source

Working example.

In your urls.py add:

 from django.views.defaults import page_not_found urlpatterns = patterns('', url(r'^profiles/$', page_not_found), ) 
+2
source

Source: https://habr.com/ru/post/1386430/


All Articles