Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raise Http404 in url pattern

I'm trying to override a url in django-profiles to raise a 404 instead of passing to the view.

I'm looking for something along the lines of:

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

But the above doesn't work. Is this sort of thing possible?

I know I can write a new view that raises a 404, but I'd rather not if I can do it in the url pattern directly.

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

like image 642
Riley Watkins Avatar asked Dec 14 '11 23:12

Riley Watkins


2 Answers

Starting from Django 1.9, the page_not_found view now "accepts a second parameter, the exception that triggered the error." So the solution proposed above by Christoffer requires some minor change:

from django.views.defaults import page_not_found

urlpatterns = patterns('',
  url(r'^profiles/$', page_not_found, {'exception': Exception('Not Found')}),
)

Hope this helps.

like image 74
Rafid Avatar answered Oct 10 '22 07:10

Rafid


Just use the view django.views.defaults.page_not_found.

like image 21
aganders3 Avatar answered Oct 10 '22 08:10

aganders3