Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one create a custom 403 page in Django?

Following this documentation (https://docs.djangoproject.com/en/dev/topics/http/views/#customizing-error-views), I am trying to create a custom error 403 page for my Django application.

I have created an error handler in my URLConf:

handler403 = 'courses.views.error403'

I have also created a view that handles error 403 called error403:

def error403(request):
    '''
    Default view for error 403: Inadequate permissions.
    '''
    return render_to_response('utility/mustLogin.html', {'user': request.user})

However, this view is simply not rendering. When I instigate an error 403 on purpose, the default browser 403 page merely appears as opposed to my template.

Default browser 403

Am I forgetting to perform some action? Thank you.

like image 299
dangerChihuahua007 Avatar asked Feb 04 '26 04:02

dangerChihuahua007


2 Answers

The handler403 is new in the development version. If you're using an older version (<= 1.3) this option is not yet implemented.

There are some ways of accomplishing that in older versions, as described here. It involves a new middleware, a custom exception and the HttpResponseForbidden. Don't know how well it would integrate with the rest of your project, though...

like image 99
mgibsonbr Avatar answered Feb 05 '26 20:02

mgibsonbr


Google Chrome will replace it with their generic one if the returned page is less than 512 bytes in size. They do the same with 404 responses as well.

For what it's worth Internet Explorer does the same thing.

If you make your response greater than 512bytes then all shall be well.

like image 41
Mike Scott Avatar answered Feb 05 '26 20:02

Mike Scott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!