Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify the name of the form variable used in the template of a FormView? (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 like this:

{{ my_contact_form }}?

This would be the forms equivalent of object_context_name(for models).

like image 715
Bentley4 Avatar asked Mar 25 '13 11:03

Bentley4


1 Answers

You could 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
like image 67
Pavel Anossov Avatar answered Oct 06 '22 22:10

Pavel Anossov