Answer the question
In order to leave comments, you need to log in
Why is form.cleaned_data empty even though form.data has two keys?
Request processing: (on line pass len(form.data) = 2 and 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'}))
<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>
Answer the question
In order to leave comments, you need to log in
form.is_valid()
if everything is fine, form.cleaned_data will appear
Proof: https://docs.djangoproject.com/en/1.7/topics/forms...
"A Form instance has an is_valid() method, which runs validation routines for all its fields.When this method is called, if all fields contain valid data, it will:
return True
place the form's data in its cleaned_data attribute."
UDP:
didn't notice that is_valid is already being called. But it's better to do otherwise
if form.is_valid():
date_min = form.cleaned_data['date_min']
date_max = form.cleaned_data['date_max']
else:
# что-то делать, если данные невалидны
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question