10

I'm trying to use my 403, 404, 500 custom templates in Django 1.5 . 404 and 500 work perfectly, but 403 still showing me the built-in Django 403 template.

I put all three templates in the root template directory in my project. They are named : 403.html, 404.html, 500.html

I also tried using:

urls.py:

 from django.utils.functional import curry
 handler403 = curry(permission_denied, template_name='403.html')

and also: urls.py:

handler403 = 'proj_name.views.my_custom_permission_denied_view'

proj_name/views.py

def my_custom_permission_denied_view(request):
    return ethoos_response('403.html', None, request)

Both methods do not work. Also in 404 and 500 I use none of these methods, just the templates inside the template directory, and they are shown.

All three suppose to work the same way according to Django's documentation. https://docs.djangoproject.com/en/1.5/topics/http/views/#the-403-http-forbidden-view

I have no idea why only 403 doesn't. Thanks.

arnon cohen
  • 485
  • 1
  • 5
  • 15
  • Works for me, there should be no requirement to overwrite any code. What's your exact django version, what does your template setting look like, have you put a breakpoint in the 403 handler to see what is going on? – Hedde van der Heide Aug 13 '15 at 06:57
  • my Django.version is (1, 5, 0, 'final', 0) . I have not yet breakpointed the handler. – arnon cohen Aug 13 '15 at 07:03
  • Any particular reason you are using Django 1.5 ? in two weeks time, it would have been unsupported for a full year https://www.djangoproject.com/download/ – e4c5 Aug 13 '15 at 07:29
  • The latest docs for the 403 handler in django are here https://docs.djangoproject.com/en/1.11/topics/http/views/#customizing-error-views – Tom Dalton Aug 11 '17 at 10:12

2 Answers2

25

For regular 403 permission denied pages, creating the 403.html template should work.

However, for CSRF errors (which also return status code 403), you should create a 403_csrf.html template instead.

Creating a 403_csrf.html template works in Django 1.10+. For earlier versions, you had to change the CSRF_FAILURE_VIEW setting to the view you want to use.

See the CSRF docs for more info.

There was a discussion about why the CSRF failure view behaves differently in the Django-developers mailing list this week.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Thank you! that was it. I also recommend on this solution example: http://stackoverflow.com/questions/26925244/django-how-to-override-the-csrf-failure-template – arnon cohen Aug 13 '15 at 07:32
1

You need to use 403_csrf.html.

alias51
  • 8,178
  • 22
  • 94
  • 166