I have a form and wish to set some default values in the fields. My original solution using the View class works
# forms.py
ResultForm(forms.ModelForm):
class Meta:
model = Result
fields = ("date", "club", "goals_for", "goals_against")
...
@staticmethod
def get_initial():
initial = {
'date': datetime.today().date(),
'goals_for': 0,
'goals_against': 0,
}
return initial
and
# views.py
class EnterResultView(View):
template = 'results/enter_result.html'
def get(self, request):
form_class = ResultForm()
form_class.initial = form_class.get_initial()
context = {
'form_class': form_class
}
return render(request, self.template, context)
But if I convert to a CreateView I cannot get the fields to initialise
# views.py
class EnterResultView(CreateView):
template_name = 'results/enter_result.html'
form_class = ResultForm
form_class.initial = form_class.get_initial()
What must I do?
[EDIT added template]
<!-- results/enter_result.html -->
...
{% for field in view.form_class %}
<tr>
<td style="text-align: right">{{ field.label_tag }}</td>
<td>{{ field }}</td>
{% if field.errors %}
{% for error in field.errors %}
<td class="form-error">{{ error }}</td>
{% endfor %}
{% endif %}
</tr>
{% endfor %}
Views with a FormMixin
[Django-doc] have a .get_initial()
method [Django-doc], you thus can override this:
class EnterResultView(CreateView):
template_name = 'results/enter_result.html'
form_class = ResultForm
def get_initial(self):
return self.form_class.get_initial()
A CreateView
constructs a form and passes this to the view as form
, you should not use view.form_class
, since that will construct another (new) form, without any parameters:
{% for field in form %}
…
{% endfor %}