Creating an user account and login system in Django is not so difficult. Django is famous for rendering faster websites. With that in mind, the number of websites are being made with Django. If you want any user interaction to your website then you must have the signup and login system in your website. Today I will show you step by step process to implement the signup and login system in Django.
Assuming that you have installed Django in your computer already and you know how to use command line through Terminal. Just follow the below steps and you will be able to create it easily.
Step-1:
You need to go to your app folder> urls.py and add the below 3 lines in urlpatterns[]. These are 3 urls for our signup system.
[php]
path('login/', views.user_login, name='login'),
path('signup/', views.user_signup, name='signup'),
path('logout/', views.user_logout, name='logout'),
[/php]
Step-2:
In your app folder, find views.py and then add the following code:
[php]
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from .forms import UserCreationForm, LoginForm
# signup page
def user_signup(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
return redirect('login')
else:
form = UserCreationForm()
return render(request, 'frontend/signup.html', {'form': form})
# login page
def user_login(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(request, username=username, password=password)
if user:
login(request, user)
return redirect('polls')
else:
form = LoginForm()
return render(request, 'frontend/login.html', {'form': form})
# logout page
def user_logout(request):
logout(request)
return redirect('login')
[/php]
Step-3:
In the app folder, we need to create the form. Create a file named forms.py and add the below codes
[php]
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class SignupForm(UserCreationForm):
class Meta:
model = User
fields = ['username', 'password1', 'password2']
class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
[/php]
Step-4:
Now we need to create Signup page for creating account of users. create a file named signup.html and put into templates folder.
[php]
<h1>Signup</h1>
<form method="POST" id="signup">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Signup</button>
<a href="{% url 'login' %}">Already have account?</a>
</form>
[/php]
Next we need to create a Login page for logging to the website. Create a file named login.html and keep in the templates folder.
[php]
<h1>Login</h1>
<form method="POST" id="login">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
<a href="{% url 'signup' %}">Dont have Account? Create</a>
</form>
[/php]
Step-5:
In your index page, you need to add the following urls ro go smoothly login and signup pages.
[php]
{% if request.user.is_authenticated %}
<a href="">{{ request.user.username }}</a>
<a href="{% url 'logout' %}">Logout</a>
{% else %}
<a href="{% url 'login' %}">Login</a>
<a href="{% url 'signup' %}">Signup</a>
{% endif %}
[/php]
Step-6:
After setting up all codes and pages above, you need to go to root project folder and run the following command:
[php]
python3 manage.py makemigrations
python3 manage.py migrate
[/php]
After running the above command line, you need to restart the server and visit your website homepage.
You will see the Login and Signup url and you are ready to create account and the username will be shown on header if you add the Step-5 codes in a navigation menu. Let me know if you have any problem implementing the signup and login system in Django.