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的等效形式(对于模型)。

最佳答案

您可以覆盖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

关于django - 如何指定在FormView模板中使用的表单变量的名称? (表单的object_context_name),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15614245/

10-16 22:34