Basic Django Login Registration Logout System

Published: 25 May 2019
on channel: Jarad Python
5,720
77

Check out Justin Mitchel's CodingForEntrepreneurs Ecommerce course. You can purchase it on Udemy or become a member at joincfe.com and find it and a ton more projects there.

In this video, I'm trying to walk my future self through how to create a basic Django login, registration and logout system. Every time I get to this step in a Django project, I feel intimidated because I can never remember how I did it the last time.

That's why I'm trying to go through each step in the process here in this video. This is a basic Django login system. In most cases, you'll want a separate Django accounts directory or, even easier, just install Django AllAuth.

Anyways, here's the Django login register logout code:

urls.py
path('login/', login_page, name='login'),
path('register/', register_page, name="register"),
path('logout/', logout_page, name='logout')

forms.py
class LoginForm(forms.Form):
username = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))
password = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'form-control'}))

class RegisterForm(forms.Form):
username = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))
email = forms.EmailField(widget=forms.EmailInput(attrs={'class': 'form-control'}))
password_first = forms.CharField(label="Password", widget=forms.PasswordInput(attrs={'class': 'form-control'}))
password_again = forms.CharField(label="Confirm password", widget=forms.PasswordInput(attrs={'class': 'form-control'}))

def clean(self):
cleaned_data = self.cleaned_data
password_one = self.cleaned_data.get('password_first')
password_two = self.cleaned_data.get('password_again')
if password_one != password_two:
raise forms.ValidationError("Passwords don't match")
return cleaned_data

def clean_username(self):
username = self.cleaned_data.get('username')
qs = User.objects.filter(username=username)
if qs.exists():
raise forms.ValidationError("The username you've chosen is unavailable.")
return username

def clean_email(self):
email_address = self.cleaned_data.get('email')
qs = User.objects.filter(email=email_address)
if qs.exists():
raise forms.ValidationError("The email address you've chosen is already registered.")
return email_address

views.py
def login_page(request):
form = LoginForm(request.POST or None)
context = {
'form': form
}
print(request.user.is_authenticated)
if form.is_valid():
print(form.cleaned_data)
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('/')
else:
print("error.......")

return render(request, "auth/login.html", context=context)

def register_page(request):
form = RegisterForm(request.POST or None)
context = {
'form': form,
}
if form.is_valid():
username = form.cleaned_data.get("username")
email = form.cleaned_data.get("email")
password = form.cleaned_data.get("password_first")
new_user = User.objects.create_user(username, email, password)
return render(request, "auth/register.html", context=context)

def logout_page(request):
logout(request)
return redirect('/')

auth/login.html, auth/register.html (Angle brackets aren't allowed in your description. so replace [ANGLE BRACKET] with an actual angle bracket)
{% extends "base.html" %}

{% block content %}
[ANGLE BRACKET]form method="POST"[ANGLE BRACKET]
{% csrf_token %}
{{ form.as_p }}
[ANGLE BRACKET]button type="submit" class="btn btn-primary"[ANGLE BRACKET]Login[ANGLE BRACKET]/button[ANGLE BRACKET]
[ANGLE BRACKET]/form[ANGLE BRACKET]
{% endblock %}


#Django #Python


Watch video Basic Django Login Registration Logout System online, duration hours minute second in high quality that is uploaded to the channel Jarad Python 25 May 2019. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 5,720 times and liked it 77 visitors.