我有一个带日期/时间输入的表单,输入界面由Bootstrap 3 datetimepicker提供。当我尝试提交表单时,它会给我一个验证错误,因为am / pm组件的时间无效。如何更改模型接受的格式作为有效日期/时间,以便正确解析am / pm组件?或者有没有办法在JS中预处理日期?
views.py
def new_appointment_existing_client(request):
form = NewAppointmentExistingClientForm()
context = {'form': form}
if request.method == 'POST':
form = NewAppointmentExistingClientForm(data=request.POST)
context['form'] = form
#form.data['date'] = datetime.datetime.strptime(form.data['date'], '%Y-%m-%d %H:%M %p')
if form.is_valid():
form.save()
return HttpResponseRedirect('/calendar/')
return render(request, 'new_appointment.html', context)
new_appointment.html
{% extends 'base.html' %}
{% block startscripts %}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.css" type="text/css" media="all" />
{% endblock startscripts %}
{% block content %}
<h3>Create an Appointment</h3>
<br>
<br>
<div class='row'>
<div class='col-md-6'>
<form method='POST' action ='/new_appointment/' id='id_new_appointment_form' enctype="multipart/form-data">
{{ form.as_p }}
{% csrf_token %}
{% if form.errors %}
<div class='form-group has-error'>
<span class='help-block'>{{ form.text.errors }}</span>
</div>
{% endif %}
<input type="submit" id='id_form_submit' value="Submit" />
</form>
</div>
</div>
{% endblock content %}
{% block endscripts %}
<script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js'></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/bootstrap.datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>
<script>
jQuery(document).ready(function($){
jQuery("#id_date").datetimepicker({
format: 'YYYY-MM-DD hh:mm a'
});
});
</script>
{% endblock endscripts %}
models.py
class Appointment(models.Model):
date = models.DateTimeField()
appointment_person = models.ForeignKey(Person)
forms.py
class NewAppointmentExistingClientForm(forms.models.ModelForm):
class Meta:
model = Appointment
fields = ('date',
'appointment_person',
)
widgets = {
'date': forms.DateTimeInput(format=['%Y-%m-%d %I:%M %p']),
}
答案 0 :(得分:0)
DateTimeField包含一个input_formats
参数,可让您指定接受的格式;你可以添加一个包含am / pm:
date = forms.DateTimeField(input_formats=['%Y-%m-%d %H:%M %p'])
答案 1 :(得分:0)
如果您在任何地方使用此日期时间格式,您应该在 settings.py 文件中覆盖两个默认设置:
DATETIME_FORMAT = '%Y-%m-%d %I:%M %p'
DATETIME_FORMAT值将是模板中用于渲染日期时间字段的默认格式
对于验证,我建议重写默认的DATETIME_INPUT_FORMATS元组,这些元素用于通过按顺序测试来验证日期时间字段。 只需添加您想要的格式作为此元组的第一个元素,我的意思是DATETIME_INPUT_FORMATS [0]。
默认为
DATETIME_INPUT_FORMATS (
'%Y-%m-%d %H:%M:%S', # '2006-10-25 14:30:59'
'%Y-%m-%d %H:%M:%S.%f', # '2006-10-25 14:30:59.000200'
'%Y-%m-%d %H:%M', # '2006-10-25 14:30'
'%Y-%m-%d', # '2006-10-25'
'%m/%d/%Y %H:%M:%S', # '10/25/2006 14:30:59'
'%m/%d/%Y %H:%M:%S.%f', # '10/25/2006 14:30:59.000200'
'%m/%d/%Y %H:%M', # '10/25/2006 14:30'
'%m/%d/%Y', # '10/25/2006'
'%m/%d/%y %H:%M:%S', # '10/25/06 14:30:59'
'%m/%d/%y %H:%M:%S.%f', # '10/25/06 14:30:59.000200'
'%m/%d/%y %H:%M', # '10/25/06 14:30'
'%m/%d/%y', # '10/25/06'
)
首先使用您的格式&#39;%Y-%m-%d%I:%M%p&#39; 更新它:
DATETIME_INPUT_FORMATS (
'%Y-%m-%d %I:%M %p',
'%Y-%m-%d %H:%M:%S', # '2006-10-25 14:30:59'
'%Y-%m-%d %H:%M:%S.%f', # '2006-10-25 14:30:59.000200'
'%Y-%m-%d %H:%M', # '2006-10-25 14:30'
'%Y-%m-%d', # '2006-10-25'
'%m/%d/%Y %H:%M:%S', # '10/25/2006 14:30:59'
'%m/%d/%Y %H:%M:%S.%f', # '10/25/2006 14:30:59.000200'
'%m/%d/%Y %H:%M', # '10/25/2006 14:30'
'%m/%d/%Y', # '10/25/2006'
'%m/%d/%y %H:%M:%S', # '10/25/06 14:30:59'
'%m/%d/%y %H:%M:%S.%f', # '10/25/06 14:30:59.000200'
'%m/%d/%y %H:%M', # '10/25/06 14:30'
'%m/%d/%y', # '10/25/06'
)
现在,默认渲染和验证应该按预期工作,而不会在日期时间字段中添加任何参数。
保持日期时间字段清洁:
date = models.DateTimeField()