Django Basic login logout tutorial

For each large or medium web application, the login logout system is very common. It is required to handle user management and offer user specific services.

In Django, web developers can implement their own authentication system or they can simply go with inbuilt authentication system of Django.

Now if you are really confident about web development then only you can develop your custom authentication system. Otherwise, it may not be a secure authentication system.

Must Read: Django Stylish login logout tutorial

But if you are a beginner level web developer then you can simply use Django inbuilt authentication system. It is secure and easy to implement.
In this tutorial, you will learn how to configure login logout functionality using inbuilt user authentication system of Django.

Before starting assuming you have at least basic knowledge of Django. Though I will explain everything in basic level only.

Django Project Setup

Let’s create a Django project to work with. Type below command in cmd to create a Django project.
django-admin startproject login_app
Note: Giving my project name login_app
After executing in your command it will create a folder in your working directiory. The folder name is login_app.
Now inside that folder two things will be there.
1.    Another folder named login_app (default python files inside this folder are listed below)
So default folder structure for entire project should looks like below:
├── login_app
│   ├──
│   ├──
│   ├──
│   └──
Now inside main project folder create another folder called templates and do the following steps.
1.    Create an HTML file named home.html (code for home.html will share below)
2.    Create another folder inside templates folder named registration
3.    Inside registration folder create another HTML file named login.html (code will share below)
So now folder structure for entire project should looks like below:
├── templates
|   ├── home.html
|   ├── registration
|         └── login.html
├── login_app
│   ├──
│   ├──
│   ├──
│   └──



Now inside login_app (application folder) folder you need to edit and
In you need to add two things
1.    Add template folder address inside TEMPLATES list dictionary (inside DIR key). By default it will be like: ‘DIRS’: [],
        ‘BACKEND’: ‘django.template.backends.django.DjangoTemplates’,
        # Adding Template folder address (edit1)
        ‘DIRS’: [os.path.join(BASE_DIR, ‘templates’),],
        ‘APP_DIRS’: True,
        ‘OPTIONS’: {
            ‘context_processors’: [
2.    Add below two lines at the end of to use home.html as default home page
Now copy and paste below code to your (After removing everything from

Also Read:  Deploy Object detection model using Flask

Once you have done above steps successfully, run below command in cmd to migrate
python migrate
And run Django server by executing below command in cmd
python runserver
Now open in your web browser. You can see a link is showing called login. If you click on it will redirect you to the login form, where you need to provide user name and password.

Create user Djanog

But you can not log in now as you have not created any authenticated user name with password right. So lets create a user from django admin page.

To do so first you should create admin user name and password for your project. To do that type below command in cmd.
python createsuperuser
Once you execute above command, it will ask for
1.    Username (which you needs to provide to login as admin). Give some name and enter.
2.    Email address
3.    Password
django create super user
Now run server again and open . Now login with your admin username and password (which you have created just above).
After login, create user by providing Username and Password and save it.

django create user

Now try to login here by providing user name and password which you have just created. It should login successfully and logout.


Also Read:  Upload and Display CSV file in html table Flask Python


In this tutorial you have learned you to create basic login logout system by using default authentication system of Django. In my next article I will show you how to make this login and logout system stylish and attractive.
If you have any question or suggestion regarding this topic see you in comment section. I will try my best to answer.

Similar Read:

Leave a comment