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.

Reply via email to