Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirection + 403 error

I am searching for a way to have something like that :

return HttpResponseForbiddenRedirect(reverse("view_name"))

an HttpResponse which redirect to a view (with its name) but still throw a 403 error

I tried to do something like that :

class HttpResponseForbiddenRedirect(HttpResponse):
    def __init__(self, redirect_to):
        super(HttpResponseForbiddenRedirect, self).__init__()
        self['Location'] = iri_to_uri(redirect_to)
        self.status_code = 403

But it didn't work. For some reason I don't understand, I don't get any content

like image 713
BlueMagma Avatar asked Dec 21 '22 19:12

BlueMagma


1 Answers

It doesn't work because you can't have a 403 response that is also acted upon as if it is a 302 response.

The HTTP spec tells browsers how to handle certain status codes, and so a browser getting a 403 won't bother to look to see if there's a Location header in the way that it would do with a 302.

If you want to redirect someone from X to Y because they're not allowed to see X, then just issue a standard 302, and do something like setting a message (using django.contrib.messages) to inform the user of why they've been redirected, or redirect them to a page that explains what is going on.

like image 186
Steve Jalim Avatar answered Dec 26 '22 14:12

Steve Jalim