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.