Hello World, Welcome to ProjectsPlaza.com. Today we will create login signup and logout with the user model in Django 3. In this tutorial, we will use the user model which comes with Django. Signup functionality does not come with Django but provides a UserCreationForm class to create signup functionality. This is a small but full-fledged app. You can download this app from GitHub. I will create an accounts app in an existing project. I have divided this tutorial into the following parts:
- Create accounts app
- Add accounts app in the project
- Url setting for accounts app
- Create a login view
- Create Signup view
- Login and Logout redirect setting
I have assumed that you have installed django and you have already a project. If not then please see my todo tutorial.
Create accounts app
In this step, we will create accounts app with the following command
py manage.py startapp accounts
Add accounts app in the project
In this step, we will connect our newly created app with a project in projects/settings.py
INSTALLED_APPS = [ ... 'accounts', ]
Url settings for accounts app
In this step, we will connect our accounts app URL with the project URL. Open project/urls.py and add the following code.
urlpatterns = [ ... path('accounts/',include('django.contrib.auth.urls')), path('accounts/',include('accounts.urls')), ]
Here we have attached two paths. One is from Django auth functionality and another is from the accounts app. We need to create accounts/urls.py and add the following code.
from django.urls import path from .views import SignUp,home urlpatterns=[ path('home/',home,name='home'), path('signup/',SignUp.as_view(),name='signup'), ]
In the next section, we will create a login, signup, and home views.
Create a login view
When we try to access http://127.0.0.1:8000/accounts/login, it will try to find associated login file in accounts/templates/registration/login.html. So let’s create the login.html file in the relative folder and add the following code.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Login</title> </head> <body> <h3>Login</h3> <hr/> <form action="." method="post"> {% csrf_token %} <table> {{form.as_table}} <tr> <td colspan="2"> <hr/> <button colspan="2">Submit</button> </td> </tr> </table> </form> </body> </html>
When you run the server, you will see the following screenshot.
Create Signup view
Open accounts.views.py file and add the following code in it.
from django.shortcuts import render from django.urls import reverse_lazy from django.views import generic from django.contrib.auth.forms import UserCreationForm def home(request): return render(request,'home.html') # Sign Up class SignUp(generic.CreateView): form_class=UserCreationForm success_url=reverse_lazy('login') template_name='registration/signup.html'
As you can see, we have created two methods. One if for the home template and another is for the signup template. When the user successfully login, logout, or signup then we will redirect to this home view.
When you access the http://127.0.0.1:8000/accounts/signup
, You will see the following screen
Login and Logout redirect Setting
In this step, we will set up redirect after a successful login or after successful logout. Open project settings.py file and add the following next-to-last-line.
LOGIN_REDIRECT_URL='home' LOGOUT_REDIRECT_URL='home'
You can see that we are redirecting at home after login or log out successfully.
I hope you are enjoying my tutorial. Please add your feedback in the comment section.