为什么我会收到错误?
这是我的forms.py:
class filtrosEntrada(forms.Form):
fechaInicio = forms.DateField(widget=forms.DateTimeInput(attrs={'class':'form-control'}),required=True,label='Fecha de inicio: ')
fechaFin = forms.DateField(widget=forms.DateTimeInput(attrs={'class':'form-control'}),required=True,label='Fecha final ')
这是我的views.py
def reporte_salida_view(request):
if request.user.is_authenticated(): # se genera el reporte pdf con los datos necesarios
if request.method == "POST":
fechaInicio = datetime.strptime(request.POST['fechaInicio']+' 00:00',"%d/%m/%y %H:%M")
fechaFin = datetime.strptime(request.POST['fechaFin']+' 23:59',"%d/%m/%y %H:%M")
最后是我的template.html
<form id='formularioEntrada' action="." method="POST">
{% csrf_token %}
<div class="row">
<div class="col-md-3">
{{ form.fechaInicio.label_tag}}
{{ form.fechaInicio}}<br>
{{ form.fechaInicio.errors}}
</div>
<div class="col-md-3">
{{ form.fechaFin.label_tag}}
{{ form.fechaFin}}
</div>
</form>
错误就是这一行
fechaFin = datetime.strptime(request.POST['fechaFin']+' 23:59',"%d/%m/%y %H:%M")
我正在使用python 2.7和Django 1.6
答案 0 :(得分:1)
当request.POST['fechaFin']
为空字符串时,您会收到该错误;例如表单字段已清除:
>>> from datetime import datetime
>>> datetime.strptime(' 23:59', "%d/%m/%y %H:%M")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data ' 23:59' does not match format '%d/%m/%y %H:%M'