How to Create a Login Page for a Website with Python in Django



Python


In this article, we show how to create a login page for a website with Python in Django.

Since this is a page that logs in users, we have made the assumption that you already know how to create a signup page.

So a signup page is a page where visitors can go to sign up and get an account for the website.

A login page is for users who already have accounts with your website. The page is simply to get into their account.

If you want to know how to create a signup page with Django, click the following link: How to Create a Signup Page for a Website with Django. This sets the foundation. The login page is much simpler.

So let's now go into the code of creating a login page with Django.

login.html Template Page

So the first thing we're going to do is create the template file of the login page. We will call this page, login.html.

This page will simply have a header that says Login Page and have the login form consisting of 2 fields of just username and password.

This is shown in the following code below.



So in this page, we have our form.

It's a very simple page.


forms.py File

Next, we will go to our forms.py file, which contains our form.

In the template file, we specified the variable form, but we haven't created it. In the forms.py file, we create the form.

We call this form, Loginform

This is shown below.



So this is a very simple form.

In every forms.py file, you need to import forms from django.

After this, we create the form, Loginform, which is an instance of the Form class.

This form just has 2 fields: username and password. That's all we need to log in a user.

The username can be a maximum of 25 characters and the password can be a maximum of 30 characters.


urls.py File

So we're going to create our separate login page on the URL /siteusers/login

We are in the siteusers app, so we open up the urls.py file in this app and then have the following code, as shown below.



So we will go to the login page with the URL http://IP_address/siteusers/login





views.py File

Lastly, we need our views.py file.

The contents of the views.py file is shown below.



We have to import a number of things, including render to render a template page, the Loginform form from the forms.py file, and User from django.contrib.auth.models to create users.

We call the function pagelogin(). You can call the function anything you like but do not call it login(), because Django already has a built-in function called login(). If you name the function-based view login(), it won't know which one you are referring to and the script won't work. So name this function anything other than login().

We then have 2 variables, uservalue and passwordvalue. We set these equal to empty strings to give the variables global scope.

We set the variable, form, to Loginform, which is what we created in the forms.py file for user login.

If the form is valid, we set the uservalue variable equal to the username field. We set the passwordvalue equal to the password field.

We then authenticate the user. Django has a built-in authenticate function. We authenticate the user based on the username and password that s/he has supplied.

If the user is not None (which is what it would be if the user doesn't exist), then we log the user in with the login() function. Into this function, we pass in request and user.

Into the context dictionary, we pass in form and an error message. Now this is not really the way it can be done. Django has built-in messages of success, but to keep it simple, we will just have it this way.

If the user is None, then this means that the username and password combination is incorrect.

And this is how you can create a login page for a website with Python in Django.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...