Creating a login view

We will start this section by using the Django authentication framework to allow users to log in to our website. Our view should perform the following actions to log in a user:

  1. Get the username and password by posting a form
  2. Authenticate the user against the data stored in the database
  3. Check whether the user is active
  4. Log the user into the website and start an authenticated session

First, we will create a login form. Create a new forms.py file in your account application directory and add the following lines to it:

from django import formsclass LoginForm(forms.Form):    username = forms.CharField()    password = forms.CharField(widget=forms.PasswordInput)

This form will be used to authenticate users against the database. ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.