RE: Using forms to handle request.GET data?
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-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of Carsten Fuchs Sent: Thursday, April 4, 2019 9:51 AM To: django-users@googlegroups.com Subject: Re: Using forms to handle request.GET data? 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 -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@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. For more options, visit https://groups.google.com/d/optout. -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@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/d181d870d6a345fd8cf9b96a5fea607e%40iss2.ISS.LOCAL. For more options, visit https://groups.google.com/d/optout.
Re: Using forms to handle request.GET data?
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 -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@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. For more options, visit https://groups.google.com/d/optout.
Re: Using forms to handle request.GET data?
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 -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@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/2374ce25-0a1e-8637-2608-af33f54ca9a9%40cafu.de. For more options, visit https://groups.google.com/d/optout.
RE: Using forms to handle request.GET data?
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-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of Carsten Fuchs Sent: Thursday, April 4, 2019 9:12 AM To: django-users@googlegroups.com Subject: Re: Using forms to handle request.GET data? 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 -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@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. -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@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/0bdfd5f6826342a4ae929ea3a73fbd77%40iss2.ISS.LOCAL. For more options, visit https://groups.google.com/d/optout.
RE: Using forms to handle request.GET data?
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 }} -Original Message- From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of Carsten Fuchs Sent: Thursday, April 4, 2019 9:12 AM To: django-users@googlegroups.com Subject: Re: Using forms to handle request.GET data? 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 -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@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. -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@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/15ab780e0c214868bde744af7b93452f%40iss2.ISS.LOCAL. For more options, visit https://groups.google.com/d/optout.
Re: Using forms to handle request.GET data?
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 -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@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.
Re: Using forms to handle request.GET data?
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 -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@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/666f4ad3-cc32-ce9d-7a21-d860223ba2a9%40cafu.de. For more options, visit https://groups.google.com/d/optout.
Re: Using forms to handle request.GET data?
On Tuesday, April 2, 2019 at 2:12:50 PM UTC-7, Carsten Fuchs wrote: > > 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() > Year: id="id_year"> > 2018# Just as expected! > 2019 > 2020 > > > > >>> year_form = JahrAuswahl({})# Data is empty now, need default > values! > >>> print(year_form) > Year: 2019 # JahrAuswahl.clean_year() provides a default > value. > Year: id="id_year"> > 2018 > 2019 # BUT it doesn't make it back into > year_form.data ! > 2020 > > > > 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 > Year: id="id_year"> > 2018 > 2019# ok! > 2020 > > > > 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 > > > -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@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/661101f1-bcbd-4276-a136-5e84f67f5703%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Using forms to handle request.GET data?
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. On Wednesday, April 3, 2019 at 1:42:50 AM UTC+4:30, Carsten Fuchs wrote: > 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() > Year: id="id_year"> > 2018# Just as expected! > 2019 > 2020 > > > > >>> year_form = JahrAuswahl({})# Data is empty now, need default values! > >>> print(year_form) > Year: 2019 # JahrAuswahl.clean_year() provides a default value. > Year: id="id_year"> > 2018 > 2019 # BUT it doesn't make it back into > year_form.data ! > 2020 > > > > 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 > Year: id="id_year"> > 2018 > 2019# ok! > 2020 > > > > 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 -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@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/feb71ac9-f4b4-4ee0-a7c9-07e0a0376abc%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.