Я написал свой собственный код, как это. Сначала вам нужно создать forms.py
:
class RegisterForm(forms.ModelForm):
username = forms.CharField(max_length=100)
password1 = forms.CharField(max_length=100, widget=forms.PasswordInput)
password2 = forms.CharField(max_length=100, widget=forms.PasswordInput)
class Meta:
model = User
fields = [
'username',
'password1',
'password2',
]
def clean_password2(self):
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')
if password1 and password2 and password1 != password2:
raise forms.ValidationError('passwords do not match')
return password2
, а затем views.py
:
def register_view(request):
form = RegisterForm(request.POST or None)
if form.is_valid():
user = form.save(commit=False)
password = form.cleaned_data.get('password1')
user.set_password(password)
user.save()
new_user = authenticate(username=user.username, password=password)
login(request, new_user)
return redirect('home_app:index')
return render(request, 'accounts/forms.html', {'form': form, 'title': 'Sign Up'})