Reputation: 3677
I am trying to write a signup form in Django, but I keep getting a Validation error when I post
'date_joined': [ValidationError(['This field is required.'])
The form is based on the django user model. I have not got this field on the form because it's value can be set in code
html
<form method="post" action="{% url 'sign-up' %}">
{% csrf_token %}
<h1>Bid for Game - Sign Up</h1>
Your first and last names will be used to help your friends identify you on the site.
<table>
<tr>
<td>{{ signup_form.first_name.label_tag }}</td>
<td>{{ signup_form.first_name }}</td>
</tr>
<tr>
<td>{{ signup_form.last_name.label_tag }}</td>
<td>{{ signup_form.last_name }}</td>
</tr>
<tr>
<td>{{ signup_form.username.label_tag }}</td>
<td>{{ signup_form.username }}</td>
</tr>
<tr>
<td>{{ signup_form.email.label_tag }}</td>
<td class='email'>{{ signup_form.email }}</td>
</tr>
<tr>
<td>{{ signup_form.password.label_tag }}</td>
<td><class='password'>{{ signup_form.password }}</class></td>
</tr>
<tr>
<td>Confirm password: </td>
<td class='password'>{{ signup_form.confirm_password }}</class</td>
</tr>
</table>
<button type="submit" class="btn btn-success">Submit</button>
</form>
forms.py
class SignupForm(forms.ModelForm):
first_name = forms.CharField(label='First name', max_length=30)
last_name = forms.CharField(label='Last name', max_length=30)
username = forms.CharField(label='User name', max_length=30)
email = forms.EmailField(label='Email address', widget=forms.EmailInput(attrs={'class': 'email'}))
password = forms.PasswordInput()
confirm_password=forms.CharField(widget=forms.PasswordInput())
date_joined = forms.DateField()
class Meta:
model = User
fields = '__all__'
widgets = {
'password': forms.PasswordInput(),
'password_repeat': forms.PasswordInput(),
}
@staticmethod
def initialise(request):
"""Return a dict of initial values."""
initial = {
'first_name': request.POST['first_name'],
'last_name': request.POST['last_name'],
'username': request.POST['username'],
'email': request.POST['email'],
'date_joined': datetime.datetime.today()
}
return initial
views.py
class SignUp(View):
url = "users/signup.html"
form_context = {
'signup_form': SignupForm,
}
def get(self, request):
context = self.form_context
return render(request, self.url, context)
def post(self, request):
signup_form = SignupForm()
signup_form.initial = SignupForm.initialise(request)
context = {'signup_form': signup_form}
form = SignupForm(request.POST)
if form.is_valid():
print('valid')
else:
print(form.errors.as_data())
print('invalid')
As date_joined is a required field, where do I set it?
Upvotes: 0
Views: 632
Reputation:
So a lot of times, I see people use things and then override everything that thing does for them for free. The form and view above can be reduced to something akin to this (untested, but it's a good enough start):
class SignupForm(forms.ModelForm):
password = forms.CharField(widget=PasswordInput)
confirm_password = forms.CharField(widget=PasswordInput)
class Meta:
model = User
exclude = ("date_joined",)
from django.views.generic import CreateView
class SignupView(CreateView):
form_class = SignupForm
def form_valid(self, form):
data = form.cleaned_data
password = data.pop('password')
confirm_password = data.pop('confirm_password')
if password != confirm_password:
form.add_error('Passwords do not match')
return self.form_invalid(form)
user = User.objects.create_user(**data)
user.set_password(password)
user.save()
Upvotes: 1