Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set initial values on a Django form using CreateView

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 %}
like image 629
Psionman Avatar asked Oct 19 '25 18:10

Psionman


1 Answers

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 %}
like image 191
Willem Van Onsem Avatar answered Oct 21 '25 07:10

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!