0% found this document useful (0 votes)
9 views11 pages

32 - Dars. Auth

The document outlines the steps to create a user registration system in Django, including setting up URLs, forms, and views. It details the creation of a registration form using Django's UserCreationForm and how to handle form submissions in the views. Additionally, it includes instructions for implementing login functionality and handling errors in the login process.

Uploaded by

bobomurodovd177
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views11 pages

32 - Dars. Auth

The document outlines the steps to create a user registration system in Django, including setting up URLs, forms, and views. It details the creation of a registration form using Django's UserCreationForm and how to handle form submissions in the views. Additionally, it includes instructions for implementing login functionality and handling errors in the login process.

Uploaded by

bobomurodovd177
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Auth

● Birinchi bo`lib urls.py da manzil yaratamiz.


● So`ng esa forms.py faylni yaratib ro`yhatdan o`tish uchun inputlarni
yaratamiz.
● views.py faylida funksiya yaratamiz

SETTINGS.PY faylida esa Django bizga ro`yhatdan o`tish uchun tayyor


inputlarni taqdim etadi.
URLS.py

path(‘register/’, views.register, name=”register”)


from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User
class RegisterForm(UserCreationForm):
username = forms.CharField(label="Username", required=True,
widget=forms.TextInput(attrs={'class' : 'form-control footer-input margin-b-
20'}))
password1 = forms.CharField(label="Parol", required=True,
widget=forms.PasswordInput(attrs={'class' : 'form-control footer-input margin-
b-20'}))
password2 = forms.CharField(label="Takroriy parol", required=True,
widget=forms.PasswordInput(attrs={'class' : 'form-control footer-input margin-
b-20'}))
email = forms.EmailField(label = "Email", required=True,
widget=forms.TextInput(attrs={'class' : 'form-control footer-input margin-b-
20'}))
first_name = forms.CharField(label = "Ism", required=True,
widget=forms.TextInput(attrs={'class' : 'form-control footer-input margin-b-
class Meta:
model = User

fields = ("first_name", "last_name", "email", "username", "password1",


"password2", )

def save(self, commit=True):


user = super().save(commit=False)
user.email = self.cleaned_data["email"]
user.first_name = self.cleaned_data["first_name"]
user.last_name = self.cleaned_data["last_name"]

if commit:
user.save()
return user
VIEWS.PY
def register(request):
if request.method == 'POST':
form = RegisterForm(request.POST)
if form.is_valid():
form.save()
#log the user in
return redirect('index')
else:
form = RegisterForm()
return render(request, 'alphauzApp/register.html',
{'form':form})
REGISTER.html
<form action="{% url 'register' %}" method="POST">
{% csrf_token %}
<label for="">{{form.username.label}}</label>
<span class="input">{{form.first_name}}</span>
<span class="input">{{form.last_name}}</span>
<span class="input">{{form.username}}</span>
<span class="input">{{form.email}}</span>
<span class="input">{{form.password1}}</span>
<span class="input">{{form.password2}}</span>
<button>Ro`yhatdan o`tish</button>
<button><a href="">Kirish</button></a>
<button><a href="{% url 'index' %}">
Asosiy sahifa
</button></a>
</form>
LOGIN
Asosiy urls.py faylida biz djangoda oldindan sozlangan accaounts ni qo`shishimiz
lozim.

path('accounts/',
include('django.contrib.auth.urls')),
REGISTRATION/LOGIN.HTML
<form method="post" action="{% url 'login' %}">

{% csrf_token %}

{{ form.username.label_tag }}

{{ form.username }}

{{ form.password.label_tag }}

{{ form.password }}

<input type="submit" value="login" />

</form>
ERRORS

{% if form.errors %}
<p>Login yoki parol xato</p>
{% endif %}

<p>
<a href="{% url 'password_reset' %}">
Parolni esizdan chiqardizmi?
</a>
</p>
LOGIN_REDIRECT_URL = '/'

LOGOUT_REDIRECT_URL = '/'
Registrasiya mavzusiza qo`shimcha ma`lumotlar.

https://docs.djangoproject.com/en/3.0/topics/auth/default/

You might also like