Django provides built-in views for logging in and out users, using the LoginView and LogoutView classes.
Logging In
Django’s LoginView provides a simple way to log users into the system. It accepts a username and password, validates them, and creates a session for the user.
Example of Using LoginView:
- The default template
registration/login.htmlis used to render the login form.
Customizing the Login Template:
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Log In</button>
</form>
Logging Out
To log out a user, Django uses the LogoutView which ends the user’s session.
Example of Using LogoutView:
- This automatically logs the user out and redirects them to the home page (or a page defined in
LOGOUT_REDIRECT_URL).