Error: Django form field is required while content has been entered in the form
View
def work_entry(request):
if request.method == "POST":
form = WorkHoursEntryForm(data=request.POST)
if form.is_valid():
form.save()
return redirect('home')
else:
print(form.errors)
else:
form = WorkHoursEntryForm()
return render(request, 'Work_Data_Entry.html', context={'form': form})
Form
class WorkHoursEntryForm(forms.ModelForm):
time_worked = forms.CharField(widget=forms.TextInput(
attrs={'class': 'form-control', 'id': "entry_time_worked", 'type': 'text'}))
shift_num = forms.CharField(widget=forms.TextInput(
attrs={'class': 'form-control', 'id': "shift_num", "type": "text"}))
class Meta:
model = WorkHoursEntry
fields = ('shift_number', 'time_worked_entry')
Model:
class WorkHoursEntry(models.Model):
entry_date = models.DateField(auto_now=True)
entry_time = models.TimeField(auto_now=True)
time_worked_entry = models.CharField(max_length=50)
shift_number = models.CharField(max_length=10)
I have entered the required data into the form but the form error i keep getting is
ul class="errorlist"shift_number ul class="errorlist" id="id_shift_number_error"This field is required. id=time_worked_entry ul class="errorlist" id="id_time_worked_entry_error">This field is required.
and i can't figure out how to fix it as the inputs are filled out but i get this error. does anyone see something i am missing. Any help would be great.
I fixed it, apparently i just needed to have the variable names i put for the form tags have the same name as what i named the variable names for the model fields. like
shift_number = models.CharField(max_length=10)
shift_number = forms.CharField(widget=forms.TextInput(
attrs={'class': 'form-control', 'id': "shift_num", "type": "text"}))