By default, when using the @login_required decorator, Django performs a 302 (temporary) redirect when redirecting a non-authenticated user to the login page. I work in conjunction with an SEO company (I know nothing of the topic myself) and he insists that the 301 (permanent) redirect is essential to the work that he is doing.
Is there anyway to force Django to perform a 301 redirect while using the @login_required decorator?
Thanks again.
The
@login_requireddecorator uses theredirect_to_loginview, which returns a DjangoHttpResponseRedirectobject to redirect the user to the login page. This object represents, as you mention, a 302 redirect. There is an alternative redirect object, theHttpResponsePermanentRedirect, though you would need to write your own decorator which uses this instead.Writing your own decorator is, of course, possible. It would be bad practice though, in my opinion. Not least because it ties your app to a particular implementation of the authentication module, but also because a 302 redirect is actually the correct one to use in this case.
The fact is that the page has not “moved permanently”. Instead, the user simply needs to authenticate himself/herself before accessing the same URL once again. For this reason, the redirect is not a permanent one, as the page has not actually “moved”.