raise Http404("my message")

594 views
Skip to first unread message

Aljosa Mohorovic

unread,
Jul 3, 2008, 11:40:16 AM7/3/08
to Django users
if i call raise Http404("my message") and i set custom view to handle
404 errors how do i get "my message" in that view?

Aljosa

Malcolm Tredinnick

unread,
Jul 3, 2008, 11:47:06 AM7/3/08
to django...@googlegroups.com

On Thu, 2008-07-03 at 08:40 -0700, Aljosa Mohorovic wrote:
> if i call raise Http404("my message") and i set custom view to handle
> 404 errors how do i get "my message" in that view?

You don't. The default 404 handler serves up a template and passes it
the request path, that is all.

If you want a more customised look, then don't raise the Http404
exception, but rather return a HttpResponseNotFound(), containing
whatever template you like. This might mean you have to catch the
Http404 exception at some point in your view code if it's raised from
the internals and return the right response, rather than letting it go
through to the default handler.

Regards,
Malcolm


Norman Harman

unread,
Jul 3, 2008, 12:04:21 PM7/3/08
to django...@googlegroups.com
Aljosa Mohorovic wrote:
> if i call raise Http404("my message") and i set custom view to handle
> 404 errors how do i get "my message" in that view?

I was gonna say look at the default 404 template. But, in my django
(newforms admin branch) /django/contrib/admin/templates/admin/404.html
there is no template var used :(

Checking docs
http://www.djangoproject.com/documentation/request_response/#the-http404-exception
I find no answer :(


Digging through the code I eventually found
c = Context({
'root_urlconf': settings.ROOT_URLCONF,
'request_path': request.path[1:], # Trim leading slash
'urlpatterns': tried,
'reason': str(exception),
'request': request,
'request_protocol': request.is_secure() and "https" or "http",
'settings': get_safe_settings(),
})

So, it looks like "reason" is the template var you want.


--
Norman J. Harman Jr.
Senior Web Specialist, Austin American-Statesman
___________________________________________________________________________
You've got fun! Check out Austin360.com for all the entertainment
info you need to live it up in the big city!

Reply all
Reply to author
Forward
0 new messages