Django 1.10 way
For Django 1.10, released in August 2016, a new parameter called redirect_authenticated_user was added to the view based on the login() function present in django.contrib.auth [1].
example
Suppose we have a Django application with a file named views.py and another file called urls.py The urls.py file will contain this Python code:
#
From this urlpatterns file urlpatterns part in the urlpatterns variable urlpatterns is as follows, which uses the already mentioned redirect_authenticated_user parameter with the value True :
url(r'^login/', auth_views.login, name='login', kwargs={'redirect_authenticated_user': True}),
Note that the default value for redirect_authenticated_user is False .
Django 1.11 way
For Django 1.11, released in April 2017, the LoginView view of the LoginView classes is based on the login() function [2], which gives you two options:
- Use the same Django 1.10 method that has just been described above, which is a positive point, since your current code will continue to work fine. If you tell the Python interpreter to display warnings, for example, by running the command
python -Wd manage.py runserver in the python -Wd manage.py runserver directory of your Django project, and then going to the login page with a web browser, you will see the same thing. the console terminal displays a warning message, for example:
/usr/local/lib/python3.6/site-packages/django/contrib/auth/views.py:54: RemovedInDjango21Warning: The login () view is replaced by the loginView () class based one.
- Use the new
Django 1.11 way , which will make your code more modern and compatible with future releases of Django. With this option, the above example will now look like this:
example
We again assume that we have a Django application with a file named views.py and another file called urls.py The urls.py file will contain this Python code:
#
From this file, the corresponding part in the urlpatterns definition of the variable is as follows, which again uses the already mentioned redirect_authenticated_user parameter with True value, but pass it as an argument to the as_view method of the LoginView class:
url(r'^login/', auth_views.LoginView.as_view(redirect_authenticated_user=False), name='login'),
Note that here the default value for the redirect_authenticated_user parameter is also False .
Recommendations
rodolfojcj Aug 08 '16 at 23:34 2016-08-08 23:34
source share