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)
a.     __init__.py
b.    settings.py
c.     urls.py
d.    wsgi.py
2.    manage.py
So default folder structure for entire project should looks like below:
login_app
├── login_app
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py
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:
login_app
├── templates
|   ├── home.html
|   ├── registration
|         └── login.html
├── login_app
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py


home.html

login.html


Now inside login_app (application folder) folder you need to edit settings.py and urls.py
In settings.py 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’: [],
TEMPLATES = [
    {
        ‘BACKEND’: ‘django.template.backends.django.DjangoTemplates’,
        # Adding Template folder address (edit1)
        ‘DIRS’: [os.path.join(BASE_DIR, ‘templates’),],
        ‘APP_DIRS’: True,
        ‘OPTIONS’: {
            ‘context_processors’: [
                ‘django.template.context_processors.debug’,
                ‘django.template.context_processors.request’,
                ‘django.contrib.auth.context_processors.auth’,
                ‘django.contrib.messages.context_processors.messages’,
            ],
        },
    },
]
2.    Add below two lines at the end of settings.py to use home.html as default home page
LOGIN_REDIRECT_URL = ‘home’
LOGOUT_REDIRECT_URL = ‘home’
Now copy and paste below code to your url.py (After removing everything from urls.py)

Also Read:  Make Desktop Notifier App using Python & Tkinter

urls.py

Once you have done above steps successfully, run below command in cmd to migrate
python manage.py migrate
And run Django server by executing below command in cmd
python manage.py runserver
Now open http://127.0.0.1:8000/ 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 manage.py 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 http://127.0.0.1:8000/admin/ . 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 http://127.0.0.1:8000/ by providing user name and password which you have just created. It should login successfully and logout.


 


Also Read:  Best Udemy Courses to learn Django with Learning Path

Conclusion

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