9
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/' 

在我的模板中,表单是这样调用的:

{{ form }}

但我怎么能这样称呼它:

{{ my_contact_form }}?

这将是等效于object_context_name(对于模型)的形式。

4

1 回答 1

9

您可以覆盖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
于 2013-03-25T12:04:29.117 回答