Programming
people_model_form_url
#model.py:
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_image = models.ImageField(upload_to='profile_images/', blank=True, null=True)
department = models.CharField(max_length=100, blank=True, null=True)
def __str__(self):
return f"{self.user.username}'s Profile"
form.py
from django import forms
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm, PasswordChangeForm
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from .models import Profile
class CreationForm(UserCreationForm):
email = forms.EmailField(required=True)
department = forms.CharField(max_length=100, required=False)
profile_image = forms.ImageField(required=False)
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2', 'department', 'profile_image']
def clean_email(self):
email = self.cleaned_data.get('email')
if not email.endswith('@kapitalbank.uz'):
raise ValidationError('You should use your bank email.')
if User.objects.filter(email=email).exists():
raise ValidationError('This email is already in use. Please use a different email.')
return email
def save(self, commit=True):
user = super().save(commit=False)
user.email = self.cleaned_data['email']
if commit:
user.save()
# Create or update the Profile
Profile.objects.create(
user=user,
department=self.cleaned_data['department'],
profile_image=self.cleaned_data['profile_image']
)
return user
class LoginForm(AuthenticationForm):
pass
class UpdateForm(forms.ModelForm):
email = forms.EmailField(required=True)
department = forms.CharField(max_length=100, required=False)
profile_image = forms.ImageField(required=False)
class Meta:
model = User
fields = ['username', 'email', 'department', 'profile_image']
def clean_email(self):
email = self.cleaned_data.get('email')
if not email.endswith('@kapitalbank.uz'):
raise ValidationError('You should use your bank email.')
if User.objects.filter(email=email).exclude(pk=self.instance.pk).exists():
raise ValidationError('This email is already in use. Please use a different email.')
return email
def save(self, commit=True):
user = super().save(commit=False)
user.email = self.cleaned_data['email']
if commit:
user.save()
# Update or create Profile
profile, created = Profile.objects.get_or_create(user=user)
profile.department = self.cleaned_data['department']
if self.cleaned_data['profile_image']:
profile.profile_image = self.cleaned_data['profile_image']
profile.save()
return user
def clean_email(self):
email = self.cleaned_data.get('email')
if not email.endswith('@kapitalbank.uz'):
#print(email)
raise ValidationError('You should use your bank email.')
if User.objects.filter(email = email).exclude(pk = self.instance.pk).exists():
raise ValidationError('This email is already in use. Please use a different email.')
return email
#urls.py
from django.urls import path
from .views import user_login, user_logout, user_create, user_update, change_password, profile_view, profile_list, my_profile
urlpatterns = [
path("login/", user_login, name="login"),
path("register/", user_create, name="register"),
path("logout/", user_logout, name="logout"),
path('update/', user_update, name = 'update-user'),
path('password-update/', change_password, name = 'update-password'),
path('profile/<str:username>/', profile_view, name='profile-user'),
path('profile-list/', profile_list, name = 'profile-list'),
path('profile/me/', my_profile, name='my-profile')
]
#path("dashboard/", dashboard, name="dashboard"),
PO
powerty
Muallif
· Staff
Iyul 27, 2026
Iyul 27, 2026
28
Ko'rishlar
0
Yoqishlar
2m
O'qildi