I want to pass the Exception info from Http404 to handler404 view and consequently the 404.html

248 views
Skip to first unread message

Kiril

unread,
Oct 30, 2011, 3:38:44 PM10/30/11
to Django developers
Hello guys,

in the past few months I have been developing a simple web site using
Django. I found Django amazing and mature framework for my needs. I am
now about to publish it in public hosting.

To my dismay the erro info I have included in Http404 calls is wasted
by Django and cannot make it's way ot the handler404 and consequently
to the 404.html template.

Here is example code from my views.py:

#is user logged in
if not request.user.is_authenticated():
raise Http404(_("It is not allowed to post anonymous comments"))

Now I want to display the localized message in the 404.html but it
seems this does not make it to the handler404 view.

Here is what I found in the Django sources -
django.core.handlers.base.get_response:

except http.Http404, e:
logger.warning('Not Found: %s' % request.path,
extra={
'status_code': 404,
'request': request
})
if settings.DEBUG:
from django.views import debug
response = debug.technical_404_response(request,
e)
else:
try:
callback, param_dict = resolver.resolve404()
response = callback(request, **param_dict)
except:


As can be clealry seen the exception "e" is only passed ot the DEBUG
handler but no to the production one "callback".

My feature request is to add keyword argument param_dict "exception"
with the value of the Http404 exception "e" that is passed over to the
handler404 code. I can make this fix in my local deployment but it is
not possible to edit my hodting provider Django instance.

This will help communicate the details of the 404 errors to end users.
Same could be applied few lines below in the handler for
exceptions.PermissionDenied.

I hope this resonates with the development concepts of Django and this
small fix can make its way in to follow on release.




-Kiril K

Mateusz Marzantowicz

unread,
Oct 31, 2011, 3:08:48 AM10/31/11
to django-d...@googlegroups.com

But why you return HTTP 404 (Not found) error in case your user is not authenticated? Not found errors are designed to handle situations where document or page is not present on the server. It has nothing to do with user authentication.

Please also see: https://docs.djangoproject.com/en/dev/topics/auth/ for django and authentication usage patterns.

Also note that one line below that block you've cited is code path for handling PermissionDenied exception.


Mateusz Marzantowicz

Ric

unread,
Nov 6, 2011, 4:36:50 PM11/6/11
to Django developers
for me a simple solution to this is issue, is that django should be
able to recognize an HttpResponse as error and return it

like:

raise HttpResponseRedirect(url)
or
raise HttpResponse(render_to_template("errors/user_not_logged"),
context = RequestContext(context), status_code = 401)

with this approch a view can raise an HttpResponse and interrupt the
view process

django should simply check if the exception is an HttpResponse class
and simply returns it.

with the new TemplateResponse, everything is made mode customizable,
because the TemplateResponse should implement a render method that
checks is settings.DEBUG is true and then return a technical response,
or return template response.

On 31 Ott, 08:08, Mateusz Marzantowicz <mmarzantow...@gmail.com>
wrote:

schinckel

unread,
Nov 6, 2011, 11:56:23 PM11/6/11
to django-d...@googlegroups.com
The way I am handling this type of thing (and this is getting into django-users territory), is to either:

- have a middleware that catches exceptions, and if they are a sub-class of HttpError, then return them,

- have a framework which handles this, often by having the CBV handle it in it's dispatch method.
Reply all
Reply to author
Forward
0 new messages