Jacobian
Jacobian

Reputation: 10812

Unable to provide a template for error 403

Well, at this moment I know how to make a custom error 404 page (and it actually works in my app). I followed the same strategy step by step to make a 403 page, but to no avail. So, this is what I have done:

#root urls.py 
handler403 = 'blog.views.custom_403'

#blog views.py
def custom_403(request):
    template = loader.get_template('blog/403.html')
    context = RequestContext(request,{})
    return HttpResponse(template.render(context))

Then, in one of my functions I have this code:

return HttpResponseForbidden()

I thought, it would render 403.html, but what I see is just a message 403 FORBIDDEN in the console.

Upvotes: 0

Views: 970

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599630

The trouble is, you are not raising a 403 exception (as you did for the 404), but simply returning a response with a status code of 403. As the docs show, you need to raise django.views.defaults.permission_denied to trigger the 403 page.

from django.core.exceptions import PermissionDenied
...
raise PermissionDenied

Upvotes: 1

Related Questions