Put forms.DateField parameter into view method

238 views Asked by At

I have following problem:

In my forms.py i have following line in class:

date_from = forms.DateField(label='date', initial='1800-01-01', widget=SelectDateWidget(years=[y for y in range(1450,2050)])) 

In template i can choose the concrete date. This parameter i want to transfer to my view.py file to the following method:

def adv_search(request):
    objects = None
    if request.GET.get('key'):
        form = AdvancedSearchForm(request.GET)
        if form.is_valid():
            repo = Repository()
            objects = list(repo.find_objects(date__gt='2012-07-23'))                            
        return render(request, 'templates/adv_search.html', {'form': form, 'objects': objects })
    return render(request, 'templates/adv_search.html', {'form': AdvancedSearchForm(), 'objects': objects})

In objects = list(repo.find_objects(date__gt='2012-07-23')) i have date__gt with concrete Date.

How i can add to date__gt the date from form.Date.Field ?

Thanks.

1

There are 1 answers

5
Thomas Orozco On

It's in form.cleaned_data:

You can access it by doing:

objects = list(repo.find_objects(date__gt=form.cleaned_data['date_from']))  

This is assuming that your form declaration looks something like:

class AdvancedSearchForm(forms.Form):
    date_from = forms.DateField(...) 
    ...