How to specify the name of the form variable used in the FormView template? (object_context_name for forms)

from forms import MyContactForm
from django.views.generic.edit import FormView 

class MyFormView(FormView):
    template_name = 'my_forms.html'                                      
    form_class = MyContactForm  
    success_url = '/thanks/' 

In my template, the form is called like this:

{{ form }}

But how can I call it as follows:

{{ my_contact_form }}?

These would be equivalent forms object_context_name(for models).

+5
source share
1 answer

You can override get_context_data:

class MyFormView(FormView):
    template_name = 'my_forms.html'                                      
    form_class = MyContactForm  
    success_url = '/thanks/' 

    # from ContextMixin via FormMixin    
    def get_context_data(self, **kwargs):
        data = super(MyFormView, self).get_context_data(**kwargs)

        data['my_contact_form'] = data.get('form')

        return data
+8
source

All Articles