- Django 2 by Example
- Antonio Melé
- 257字
- 2021-06-25 21:19:10
Using Django authentication views
Django includes several forms and views in the authentication framework that you can use straight away. The login view you have created is a good exercise to understand the process of user authentication in Django. However, you can use the default Django authentication views in most cases.
Django provides the following class-based views to deal with authentication. All of them are located in django.contrib.auth.views:
- LoginView: Handles a login form and logs in a user
- LogoutView: Logs out a user
Django provides the following views to handle password changes:
- PasswordChangeView: Handles a form to change the user password
- PasswordChangeDoneView: The success view the user is redirected to after a successful password change
Django also includes the following views to allow users to reset their password:
- PasswordResetView: Allows users to reset their password. It generates a one-time use link with a token and sends it to the user's email account.
- PasswordResetDoneView: Tells users that an email—including a link to reset their password—has been sent to them.
- PasswordResetConfirmView: Allows users to set a new password.
- PasswordResetCompleteView: The success view the user is redirected to after successfully resetting the password.
The views listed in the preceding list can save you a lot of time when creating a website with user accounts. The views use default values that you can override, such as the location of the template to be rendered, or the form to be used by the view.
You can get more information about the built-in authentication views at https://docs.djangoproject.com/en/2.0/topics/auth/default/#all-authentication-views.