Обработка запроса: (на строке pass len(form.data) = 2, а len(form.cleaned_data) = 0)
form = ReportPeriodSelectForm(request.GET)
my_bonuses = tree_node.get_invite_bonuses()
my_bonuses = sorted(my_bonuses, key=lambda x: x.line)
date_min = tree_node.contract_date
date_max = date.today()
if form.is_valid():
pass
if 'date_min' in form.cleaned_data and form._cleaned_data['date_min']:
date_min = form.cleaned_data['date_min']
if 'date_max' in form.cleaned_data and form.cleaned_data['date_max']:
date_max = form.cleaned_data['date_max']
my_bonuses = filter(lambda x: date_max >= x.bonus_date >= date_min, my_bonuses)
total = sum([x.total for x in my_bonuses if not x.defaulter_invited])
Форма:
class ReportPeriodSelectForm(forms.Form):
date_min = forms.DateField(widget=widgets.DateInput(attrs={'class': 'form-control text-center',
'placeholder': 'с',
'name': 'date_min'}))
date_max = forms.DateField(widget=widgets.DateInput(attrs={'class': 'form-control text-center',
'placeholder': 'по',
'name': 'date_max'}))
HTML:
<form action="/pyramid/profile/invite_bonuses/" class="form-horizontal form-bordered">
<!-- Datepicker for Bootstrap (classes are initialized in js/app.js -> uiInit()), for extra usage examples you can check out http://eternicode.github.io/bootstrap-datepicker -->
<fieldset>
<div class="form-group">
<label class="col-md-4 control-label" for="example-daterange1">Выберите диапазон дат:</label>
<div class="col-md-8">
<div class="input-group input-daterange" data-date-format="dd/mm/yyyy">
{{ form.date_min }}
<span class="input-group-addon"><i class="fa fa-angle-right"></i></span>
{{ form.date_max }}
</div>
</div>
</div>
<div class="form-group form-actions">
<div class="col-md-8 col-md-offset-4">
<input type="submit" value="Выбрать" class="btn btn-sm btn-primary"/>
<input type="reset" value="Очистить" class="btn btn-sm btn-warning"/>
</div>
</div>
</fieldset>
</form>