Answer the question
In order to leave comments, you need to log in
Why does form.is_valid() return false?
In short, I write user registration, all my code is below:
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']
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})
{% 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="[email protected]">
</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 %}
Answer the question
In order to leave comments, you need to log in
Why reinvent the wheel? There are ready made solutions in django for this
UserCreationForm / AuthenticationForm
PS Off.docs
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question