Basic Django Login Registration Logout System

preview_player
Показать описание

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:

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

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):
if password_one != password_two:
raise forms.ValidationError("Passwords don't match")
return cleaned_data

def clean_username(self):
raise forms.ValidationError("The username you've chosen is unavailable.")
return username

def clean_email(self):
raise forms.ValidationError("The email address you've chosen is already registered.")
return email_address

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

def register_page(request):
form = RegisterForm(request.POST or None)
context = {
'form': form,
}

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

{% block content %}
[ANGLE BRACKET]form method="POST"[ANGLE BRACKET]
{% csrf_token %}
[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
Рекомендации по теме
Комментарии
Автор

Thanks for the video. The white theme is burning my eyes tho lmaoo

veotic
Автор

Very nice....👍🏻👍🏻
Can we use mongo DB instead of Sql3Lite ?
If we use mongoDB, how to authenticate password ...

vinodsagar
Автор

brother can you please give us code for base.html

smartlearninglectures