Using forms to handle request.GET data?

36 views
Skip to first unread message

Carsten Fuchs

unread,
Apr 2, 2019, 5:12:50 PM4/2/19
to Django users
Dear Django group,

I would like to show users a form that they can use to customize the view, for example a ChoiceField from which the year of the report can be chosen. It seems straightforward to use a forms.Form to handle the request.GET data:


from django import forms

class JahrAuswahl(forms.Form):
year = forms.ChoiceField(required=False, choices=[(j, j) for j in range(2018, 2021)])

def clean_year(self):
y = self.cleaned_data['year']
if not y:
# Set a "default" value here.
y = 2019
print('Year:', y)
return y


However, the problem is with default values. Please consider this:


>>> year_form = JahrAuswahl({'year': 2018}) # Provide explicit data.
>>> print(year_form)
Year: 2018 # from JahrAuswahl.clean_year()
<tr><th><label for="id_year">Year:</label></th><td><select name="year" id="id_year">
<option value="2018" selected>2018</option> # Just as expected!
<option value="2019">2019</option>
<option value="2020">2020</option>
</select></td></tr>


>>> year_form = JahrAuswahl({}) # Data is empty now, need default values!
>>> print(year_form)
Year: 2019 # JahrAuswahl.clean_year() provides a default value.
<tr><th><label for="id_year">Year:</label></th><td><select name="year" id="id_year">
<option value="2018">2018</option>
<option value="2019">2019</option> # BUT it doesn't make it back into year_form.data !
<option value="2020">2020</option>
</select></td></tr>


Well, the problem is that with {} (that is, request.GET == {}), we get default values in year_form.cleaned_data, but we cannot use year_form in the template context to render the form fields.
Some work-around might be to use the year_form.cleaned_data to initialize a new JahrAuswahl instance:


>>> year_form = JahrAuswahl(year_form.cleaned_data) # HACK!?
>>> print(year_form)
Year: 2019
<tr><th><label for="id_year">Year:</label></th><td><select name="year" id="id_year">
<option value="2018">2018</option>
<option value="2019" selected>2019</option> # ok!
<option value="2020">2020</option>
</select></td></tr>


But this feels like a hack and really not like the proper way to do this.
How can I solve this problem in an elegant Django manner?

Best regards,
Carsten


Mohammad Etemaddar

unread,
Apr 3, 2019, 3:28:09 PM4/3/19
to Django users
Set default value:
year = forms.ChoiceField(required=False, choices=[(j, j) for j in range(2018, 2021)], default=2019)
You do not need clean_year any more I think.

Allen Stewart

unread,
Apr 3, 2019, 4:40:01 PM4/3/19
to Django users

Carsten Fuchs

unread,
Apr 4, 2019, 9:22:20 AM4/4/19
to django...@googlegroups.com
Hello,

Am 03.04.19 um 21:28 schrieb Mohammad Etemaddar:
> Set default value:
> year = forms.ChoiceField(required=False, choices=[(j, j) for j in range(2018, 2021)], default=2019)

Thanks for your reply, but unfortunately, `default` is a keyword for model fields only, not for form fields:
TypeError: __init__() got an unexpected keyword argument 'default'

Best regards,
Carsten

Carsten Fuchs

unread,
Apr 4, 2019, 10:12:36 AM4/4/19
to django...@googlegroups.com
To elaborate on this further:

Searching the web for this yields numerous hits, e.g. https://stackoverflow.com/questions/16026479/django-forms-default-values-for-bound-forms mentions some ways to address this, but I've not come across a fully satisfactory solution.

Another approach might be this:


params = {'year': '2019'} # default values
params.update(request.GET)
year_form = JahrAuswahl(params)

# Can now use year_form normally, as intended:

if not year_form.is_valid():
# Pass year_form to the view to re-render the form with errors.
return render(..., {'year_form': year_form})

# year_form is valid, now use the year_form.cleaned_data values.
# If (unrelated) request.POST data turns out to be invalid,
# we may re-render year_form in this code path as well.
# ...


Thoughts?

Best regards,
Carsten

Matthew Pava

unread,
Apr 4, 2019, 10:24:00 AM4/4/19
to django...@googlegroups.com
If you need a default year in your template context, put it there.

def get_context_data(self, request, *args, **kwargs):
context = super().get_context_data(request, *args, **kwargs)
context['default_year'] = 2019
return context

In your template, you could use this type of construct:
{{ year or default_year }}
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7cce7f43-e5e1-995a-a201-92905ba8f2bc%40cafu.de.
For more options, visit https://groups.google.com/d/optout.

Matthew Pava

unread,
Apr 4, 2019, 10:24:35 AM4/4/19
to django...@googlegroups.com
And, since you really don't want constants littering your code, you probably want something like this:
context['default_year'] = timezone.now().year

-----Original Message-----
From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of Carsten Fuchs
Sent: Thursday, April 4, 2019 9:12 AM
To: django...@googlegroups.com
Subject: Re: Using forms to handle request.GET data?

Carsten Fuchs

unread,
Apr 4, 2019, 10:32:05 AM4/4/19
to django...@googlegroups.com
Am 04.04.19 um 16:24 schrieb Matthew Pava:
> And, since you really don't want constants littering your code, you probably want something like this:
> context['default_year'] = timezone.now().year

Sure, thanks, but the year was only used to keep the example code minimal and the focus on how to handle a form that is based on request.GET data and has default values. ;-)

Best regards,
Carsten

Carsten Fuchs

unread,
Apr 4, 2019, 10:51:07 AM4/4/19
to django...@googlegroups.com
Am 04.04.19 um 16:23 schrieb Matthew Pava:
> If you need a default year in your template context, put it there.
>
> def get_context_data(self, request, *args, **kwargs):
> context = super().get_context_data(request, *args, **kwargs)
> context['default_year'] = 2019
> return context
>
> In your template, you could use this type of construct:
> {{ year or default_year }}
>

No, the goal is to have a HTTP GET based form that the user can use to customize the view, and I'm looking for an elegant way to handle this with Django forms.

The key problem is that default values for form fields are not relevant when working with POST requests (where `initial` values are used in GET requests or request.POST data in POST request), but *are* relevant for HTTP GET (request.GET) based forms. As Django forms are tailored to work with `initial` and POST data, I've not yet found a really Django-elegant way to deal with this problem.

Best regards,
Carsten

Matthew Pava

unread,
Apr 4, 2019, 11:18:34 AM4/4/19
to django...@googlegroups.com
You could try something like this:
data = request.GET.copy()
data.setdefault('year', 2019) # fill 'year' in data if it's not already set; returns 2019
year_form = JahrAuswahl(data)


-----Original Message-----
From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of Carsten Fuchs
Sent: Thursday, April 4, 2019 9:51 AM
To: django...@googlegroups.com
Subject: Re: Using forms to handle request.GET data?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fe1a5f18-7319-7358-00b4-2670e8111b08%40cafu.de.
Reply all
Reply to author
Forward
0 new messages