2👍
✅
When the FormView
creates the actual form object, it gets the arguments to pass to the form from get_form_kwargs()
:
def get_form_kwargs(self):
"""
Returns the keyword arguments for instantiating the form.
"""
kwargs = {
'initial': self.get_initial(),
'prefix': self.get_prefix(),
}
if self.request.method in ('POST', 'PUT'):
kwargs.update({
'data': self.request.POST,
'files': self.request.FILES,
})
return kwargs
Notice how it’s calling the get_initial()
on itself (the view) rather than the form. It can’t call it on the form because it’s not initialized yet. Move your method into the view and you’re good to go.
As a sidenote, use django.utils.timezone.now()
as opposed to stdlib datetime.date.today()
since it respects your django timezone settings and you may occasionally see some off-by-one quirks otherwise.
Edit: You should also update your form to use ChoiceField
, and set the defaults with timezone.now().month
and timezone.now().year
.
Happy coding.
Source:stackexchange.com