починил валидацию логина и добавил фоточку в профиль

This commit is contained in:
Андрей Дувакин 2024-04-26 18:19:56 +05:00
parent 5f76e51dec
commit f584d3f56d
3 changed files with 28 additions and 0 deletions

View File

@ -20,4 +20,9 @@
}
.btn {
margin: 5px !important;
}
.user_image {
width: 20vw;
border-radius: 2vw;
margin-bottom: 1.5vw;
}

View File

@ -38,6 +38,9 @@
<div id="help" class="form-text">{{ field.help_text }}</div>
</div>
{% endfor %}
{% if user.profile.image %}
<img class="user_image" src="{{ user.profile.image.url }}">
{% endif %}
{% if messages %}
{% for message in messages %}
<p> {{ message }} </p>

View File

@ -7,7 +7,9 @@ from django.contrib.auth.forms import (
UserChangeForm,
UserCreationForm,
)
from django.core.validators import MaxLengthValidator, RegexValidator
from django.forms import DateInput, ModelForm
from django.utils.translation import gettext_lazy as _
from users.models import Profile
@ -53,6 +55,15 @@ class SignUpForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
fields = ('username', 'email')
def clean_username(self):
username = self.cleaned_data['username']
if len(username) > 150:
raise forms.ValidationError('Максимальная длина 150 символов.')
if not all(char.isalnum() or char in '@/./+/-/_' for char in username):
raise forms.ValidationError('Можно использовать только буквы, цифры и символы @/./+/-/_.')
return username
class ProfileForm(ModelForm):
def __init__(self, *args, **kwargs) -> None:
@ -99,3 +110,12 @@ class UserForm(forms.ModelForm):
model.first_name.field.name,
model.last_name.field.name,
]
def clean_username(self):
username = self.cleaned_data['username']
if len(username) > 150:
raise forms.ValidationError('Максимальная длина 150 символов.')
if not all(char.isalnum() or char in '@/./+/-/_' for char in username):
raise forms.ValidationError('Можно использовать только буквы, цифры и символы @/./+/-/_.')
return username