Кароче, пишу я регистрацию пользователей, весь мой код ниже:
forms.py
from django.contrib.auth.models import User
from django import forms
class UserAuthenticationForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
class UserRegistrationForm(forms.ModelForm):
password = forms.CharField(label='password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Repeat password', widget=forms.PasswordInput)
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email')
def clean_password2(self):
cd = self.cleaned_data
print('loh')
if cd['password'] != cd['password2']:
print('loh')
raise forms.ValidationError('Passwords do not match.')
return cd['password2']
views.py
def loginUser(request):
if request.method == 'POST':
reg_form = UserRegistrationForm(request.POST)
print(reg_form['username'])
print(reg_form['password'])
if reg_form.is_valid():
cd = reg_form.cleaned_data
new_user = reg_form.save(commit=False)
new_user.set_password(cd['password'])
new_user.save()
print(1)
return render(request, 'friendbook/register_done.html', {'new_user':new_user})
else:
return HttpResponse('gugli')
else:
reg_form = UserRegistrationForm()
return render(request, 'registration/login.html', {'reg_form':reg_form})
login.html
{% extends 'friendbook/base.html' %}
{% load static %}
{% block extra_css %}
<link rel="stylesheet" href="{% static 'css/login_style.css' %}">
{% endblock %}
{% block content %}
<main>
<div class="main_part container-xxl w-100 main_part">
<form class="row g-3" action="" method="post">
{% csrf_token %}
<div class="col-md-6">
<label for="{{ form.first_name.id_for_label }}" class="form-label">First name</label>
<input type="text" class="form-control" name="{{ form.first_name.html_name }}" id="{{ form.first_name.id_for_label }}">
</div>
<div class="col-md-6">
<label for="{{ form.last_name.id_for_label }}" class="form-label">Last name</label>
<input type="text" class="form-control" name="{{ form.last_name.html_name }}" id="{{ form.last_name.id_for_label }}">
</div>
<div class="col-12">
<label for="{{ form.email.id_for_label }}" class="form-label">Email</label>
<input type="email" class="form-control" name="{{ form.email.html_name }}" id="{{ form.email.id_for_label }}" placeholder="ivanov2000@mail.ru">
</div>
<div class="col-12">
<label for="{{ form.username.id_for_label }}" class="form-label">Login</label>
<input type="text" class="form-control" name="{{ form.username.html_name }}" id="{{ form.username.id_for_label }}" placeholder="">
</div>
<div class="col-md-6">
<label for="{{ form.password.id_for_label }}" class="form-label">Password</label>
<input type="password" class="form-control" name="{{ form.password.html_name }}" id="{{ form.password.id_for_label }}">
</div>
<div class="col-md-6">
<label for="{{ form.password2.id_for_label }}" class="form-label">Confirm password</label>
<input type="password" class="form-control" name="{{ form.password2.html_name }}" id="{{ form.password2.id_for_label }}">
</div>
<div class="col-12">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="gridCheck">
<label class="form-check-label" for="gridCheck">
Check me out
</label>
</div>
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary">Зарегистрироваться</button>
</div>
{{ form.errors }}
</form>
</div>
</main>
{% endblock %}
Ну и в общем-то что-то не так с reg_form.is_valid() - всегда возвращает false. На сайте провожу тест, регаю пользователя, вроде все правильно ввожу, и в представлении все вроде нормально расписал. Если есть лишние пару минут, посмотрите код и одарите советом мудрым)