Django - error_403() got an unexpected keyword argument 'exception'

When the user is not allowed to see the contents of the instance, when the PermissionDenied exception was thrown, instead of forwarding it to the 404.html template, it has an error.


class OccurrenceDetail(OccurrenceModel, BaseDetailViewWithLogin):
    permission_required = ('occurrences.see_occurrence')

    def get_object(self, queryset=None):
        perm = self.request.user.has_perm(self.permission_required)
        obj = super(OccurrenceDetail, self).get_object(queryset=queryset)
        if not perm:
            raise PermissionDenied()
        return obj


handler403 = 'apps_core.core.views.error_403'


def error_403(request):
    data = {}
    return render(request,'errors/403.html', data)

1 answer

  • answered 2018-10-11 19:12 Ralf

    The 403 error view expects a second argument, which is the raised exception.

    To solve it, you can change your code to:

    def error_403(request, exception):

    or something more general

    def error_403(request, *args, **kwargs):