I am using a mixin to check if a user is authenticated before allowing them into a class based formview. It works fine, but if the user is not authenticated it redirects them to the default login path of '/accounts/profile/'. According to the documentation this path can be overriden with the use of LOGIN_REDIRECT_URL or maybe even LOGIN_URL in settings.py. However, the documentation states that both of these are deprecated as of Django 1.8 and will be removed in Django 1.10. I am on 1.9 and would like to future proof my code. How can I specify what the login url or login redirect url is using a non deprecated method? I would even be willing to abandon the mixin approach, but how would I perform a manual check upon the request of a class based view and then redirect away from it if the check fails?
class LoginCheckMixin(UserPassesTestMixin):
def test_func(self):
if 'id_token' in self.request.session:
return validate_google_token(self.request.session['id_token'])
else:
return False
class RegistrationFormView(LoginCheckMixin, FormView):
template_name = 'registration/registration_form.html'
form_class = RegistrationForm
success_url = 'results'
LOGIN_URL
and LOGIN_REDIRECT_URL
are not deprecated.
In Django 1.8, it is deprecated to use the a dotted Python path as the values for these settings. Say your login url is:
url('^login/$', views.login, name='login')
The following is deprecated,
LOGIN_URL = 'path.to.views.login'
but you can still use
LOGIN_URL = '/login/' # hardcoded url
or
LOGIN_URL = 'login' # the name of the url pattern
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.