Remove the mandatory field label 'This field is required.' and fix the bug with 'clean_email'
I use the built-in Django, 'UserCreationForm' to create a registration form, but there are unnecessary instructions 'This field is required.' that these fields are required, I want to remove them, please help, or at least translate into Russian, so that the inscription is in Russian, if you can, also help fix clean_email in forms, so that it does not always throw out this inscription after 1 test entry of an existing email here is a screenshot of the result RegisterUserForm
views.py:
class RegisterUser(CreateView):
form_class = RegisterUserForm
template_name = 'users/register.html'
extra_context = {'title': 'Регистрация'}
get_success_url = reverse_lazy('users:login')
forms.py:
class RegisterUserForm(UserCreationForm):
username = forms.CharField(label="Логин:", widget=forms.TextInput(attrs={'class': "form-input"}))
password1 = forms.CharField(label="Пароль:", widget=forms.PasswordInput(attrs={'class': "form-input"}))
password2 = forms.CharField(label="Повтор пароля:", widget=forms.PasswordInput(attrs={'class': "form-input"}))
class Meta:
model = get_user_model()
fields = {'username', 'email', 'first_name', 'last_name', 'password1', 'password2'}
labels = {
'username': 'Логин:',
'email': 'E-mail:',
'first_name': 'Имя:',
'last_name': 'Фамилия:',
'password1': 'Пароль:',
'password2': 'Повторить пароль:',
}
widgets = {
'username': forms.TextInput(attrs={'class': "form-input"}),
'first_name': forms.TextInput(attrs={'class': "form-input"}),
'last_name': forms.TextInput(attrs={'class': "form-input"}),
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.order_fields(['username', 'email', 'first_name', 'last_name', 'password1', 'password2'])
def clean_email(self):
email = self.cleaned_data['email']
if get_user_model().objects.filter(email=email).exists():
raise forms.ValidationError(("E-mail должен быть уникальным!"), code="invalid")
return email
users/register.html:
{% extends 'users/base.html' %}
{% block title %}Регистрация{% endblock %}
{% block content %}
<h1>Регистрация</h1>
<form method="post">
{% csrf_token %}
{% for f in form %}
<p><label class="form-label" for="{{ f.id_for_label }}">{{ f.label }} </label>{{ f }}</p>
<div class="form-error">{{ f.errors }}</div>
{% endfor %}
<p><button type="submit">Регистистрация</button></p>
</form>
{% endblock %}
I've used the AI and watched videos, but apparently no one has had this question in their head all this time. I'm sure it's very easy to do, but I don't know how.