我有一个django表单,它在
中具有datefieldclass SearchForm(Form):
#otherifields
birth_date = forms.DateField(widget=DateInput(attrs={'classs':'datepicker form-control'}))
在我的视图中,我得到了张贴的数据
post_data = form.cleaned_data
尝试访问
的日期date = post_data['birth_date']
但是,无论我是否在模板上设置一个日期,该值始终都不是。我将其用于日期widget的日期,通过我所有的Django项目,并且在我的Modelforms中正常工作。但它不会说错误的日期等错误。
我的django视图
def search(request):
form = SearchForm(request.POST or None)
if request.method == 'POST'::
post_data = form.cleaned_data
customers = Customer.objects.filter(first_name__icontains=post_data['first_name'],
last_name__icontains=post_data['last_name'],
middle_name__icontains=post_data['middle_name'],
gender=post_data['gender'],
email__icontains=post_data['email'],
telephone__icontains=post_data['telephone'],
work_phone__icontains = post_data['work_phone'],
mobile__icontains=post_data['mobile'],
address__icontains = post_data['address'],
region__icontains = post_data['region'],
state__icontains = post_data['state'],
municipality__icontains = post_data['municipality'],
postal_code__icontains = post_data['postal_code'],
country__icontains = post_data['country'],
)
if post_data['birth_date']:
customers = customers.filter(birth_date=post_data['birth_date'])
#其他和返回
但是,如果我print post_data['birth_date']
,我总是没有得到。怎么了?我是否缺少表格?
您的代码段
if post_data['birth_date']:
customers.filter(birth_date=post_data['birth_date'])
在search
函数的范围之外。这也可能是其价值的原因是None