my views.py:
def add_atten(request):
if request.method == 'POST':
form = AttendancenameForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('student:listatten'))
else:
print(form.errors)
else:
form = AttendancenameForm()
return render(request, 'add_atten.html', {'form': form},)
我的template-add_atten.html:
{% if attendance %}
<table>
<tr>
<th>Teacher</th>
<th>Date</th>
<th>IN-TIME</th>
<th>OUT-TIME</th>
</tr>
{% for atten in attendance %}
<tr>
<td>{{atten.teacher_ name}}</td>
<td>{{atten.date}}</td>
<td>{{atten.intime}}</td>
<td>{{atten.outtime}}</td>
<td><a href="{% url 'student:updateatten' atten.id %}">Update</a></td>
<td><a href="{% url 'student:attendel' atten.pk %}">Delete</a></td>
</tr>
{% endfor %}
my models.py:
class Attendancename(models.Model):
teacher_name = models.ForeignKey(Teachername)
date = models.DateField('Date')
intime = models.TimeField('IN-TIME')
outtime = models.TimeField('OUT-TIME')
my forms.py:
class AttendancenameForm(ModelForm):
date = forms.DateField(input_formats=['%d-%m-%y'])
intime = forms.TimeField(input_formats=['%H:%M'])
outtime = forms.TimeField(input_formats=['%H:%M'])
teacher_name = forms.ModelChoiceField(queryset=Teachername.objects.all())
class Meta:
model = Attendancename
fields = ('teacher_name', 'date', 'intime', 'outtime',)
当我想提交日期值时,它会抛出错误'输入有效日期'。但是我总是提交有效值并尝试过不同的组合,但它不起作用。请建议我修复。
如果我从forms.py中省略'input_formats'并尝试使用django中的date字段的默认值提交我的表单中的值而不是显示任何错误,而不是上面的内容。请给我一个解决方案。