We have learnt how to create django user registration form. Now in this article i am going to write about django user login form. Django comes with built in function that automatically generates user authentication form like log in form.
Django login and logout comes under django.contrib.auth.urls. If you are following my Django authentication tutorial than you will be able to easily grasp this tutorial, otherwise it will take some time to figure out about url patterns. That’s not too hard, you can do it.
Django user login form | Django login form
To use this you need to add django.contrib.auth.urls in your projects urls.py file. Open your main project’s urls.py and add these following lines.
main project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('password/', include('password_generator.urls')),
path('demo/', include('demo.urls')),
path('accounts/', include('accounts.urls')),
path('accounts/', include('django.contrib.auth.urls')), # add this line in your urls.py
]
You must be thinking about why there are two ‘accounts/’. That’s because i want my login page as ‘accounts/login/’ that’s why there are two ‘accounts/’ you can give anything in place of accounts, its your choice.
Django authentication provides functionality of login, logout, password_reset. You don’t need you specify in urls.py to open it. These are built in, you just need to open in browser you will see it opens without any burden. You don’t need to configure views.py file or anything.
Next step is to add login.html. For that we need to create a folder inside our accounts app and name it ‘registration’. IT IS VERY IMPORTANT TO KEEP THE NAME AS IT IS. You don’t need to change it. If you change the name of ‘registration’ to something else django will throw an error template does not exist. It is because Django searches for login, logout, password_reset inside registration folder.
After creating ‘registration’ folder, now create login.html page. Again name must be same.
In the previous tutorial, i have written about crispy forms, so i am not going to tell you in this post. Load crispy form tag before your html code.
{% load crispy_forms_tags %}
add bootstrap or css anything. I am not showing you whole code, just main form tag.
login.html
That’s it, you have created login page. Here is the look of final django user login form.
go to http://127.0.0.1:8000/accounts/login/ to go to login page. sign up page is at http://127.0.0.1:8000/accounts/ .Your sign up page url may be different. that does not matter.
Now you have to add login redirect page and log out redirect page. Where do you want to go after you have logged in and where you want to go after log out.
Go to your settings.py file and add these two line of code.
LOGIN_REDIRECT_URL = "/"
LOGOUT_REDIRECT_URL = "/"
Here after log in, we will go to home page and after logged out, we will also go to home page. you can add your own page, where you want to go and you can also add logout redirect url. Its upon your choice.
Django Logout url | Django user login form
Now you have created django user login form, now you also have to create logout option. You have to add link to your logout url.