Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: fill form from get method

Tags:

forms

django

Form:

class SearchJobForm(forms.Form):
    query = forms.CharField()  
    types = forms.ModelChoiceField(queryset=JobType.objects.all(), widget=forms.CheckboxSelectMultiple())

View

def jobs_page(request):
if 'query' in request.GET:
    form = SearchJobForm(request.GET)
else:
    form = SearchJobForm()
variables = RequestContext(request, {

                                     'form':form,
                                     })
return render_to_response('jobs_page.html', variables)

After i submit the form i try to get its values back in the form

 form = SearchJobForm(request.GET)

but it doesn't work(some fields dissapear). Maybe it's because of the ModelChoiceField. How do i fill the form with its values using get method?

like image 662
barin Avatar asked Dec 08 '09 18:12

barin


3 Answers

It looks like you are trying to show a pre-populated form to a user. To do that, you need to pass the initial argument to your form:

SearchJobForm(initial=request.GET)
like image 117
Harold Avatar answered Nov 22 '22 21:11

Harold


Actually, could you post your entire view method? I just tested it and doing

form = SearchJobForm(request.GET)

works fine. It has to be a problem at the surrounding code...


From your code I think you are expecting the form values to render back in HTML with the values populated... Is that how you are trying to check that the form object was populated? This won't work (and probably isn't what you want to do anyhow - you want to process the form values).

Try adding this to your view:

def jobs_page(request):
    if 'query' in request.GET:
        form = SearchJobForm(request.GET)
        if form.is_valid():
            print form.cleaned_data['query']
            print form.cleaned_data['types']
        else:
            print form.errors
    else:
        form = SearchJobForm()
    variables = RequestContext(request, {
                                 'form':form,
                                 })
    return render_to_response('jobs_page.html', variables)

Check what gets printed out.

You really should go through this trail from the django docs.

like image 24
cethegeek Avatar answered Nov 22 '22 21:11

cethegeek


The currently submitted answers do not handle the scenario in which a list of values is passed as a GET parameter for a single key. Under the hood, Django uses a MultiValueDict data structure which doesn't have a good way of getting values from a key which has multiple values. The code below compensates for that,

def get_initial(self):
    if self.request.method == 'GET':
        initial = {}
        for key, value_list in self.request.GET.lists():
            if len(value_list) <= 1:
                initial[key] = value_list[0]
            else:
                initial[key] = value_list
        return initial
    return super().get_initial()

This code was written for class based views but could be easily adopted for use within functional based views.

like image 40
Kevin Cherepski Avatar answered Nov 22 '22 20:11

Kevin Cherepski