Django 指定常规视图的上下文对象名称
我很难为django的基于类的通用视图设置上下文对象名称Django 指定常规视图的上下文对象名称,django,django-generic-views,Django,Django Generic Views,我很难为django的基于类的通用视图设置上下文对象名称 class FictiveCreateView(generic.CreateView): context_object_name = 'fictive_form' form_class = forms.FictiveForm template_name = 'fictive/create_fictive.html' def get_context_data(self, **kwargs):
class FictiveCreateView(generic.CreateView):
context_object_name = 'fictive_form'
form_class = forms.FictiveForm
template_name = 'fictive/create_fictive.html'
def get_context_data(self, **kwargs):
context = super(generic.CreateView, self).get_context_data(**kwargs)
print context
我认为设置context\u object\u name='virtualive\u form'
会更改context对象的名称。
结果它只给出了一个空对象:
{'fictive_form': None, 'form': <fictive.forms.FictiveForm object at 0x7f925807a9d0>, u'view': <fictive.views.FictiveCreateView object at 0x7f925807aa50>}
{'virtualive_form':无,'form':,u'view':}
我在这里遗漏了什么?您的模型中有
上下文\u对象\u名称
class FictiveCreateView(generic.CreateView):
context_object_name = 'fictive_form'
form_class = forms.FictiveForm
template_name = 'fictive/create_fictive.html'
model = YourModel
def get_context_data(self, **kwargs):
context = super(generic.CreateView, self).get_context_data(**kwargs)
print context
在您的模板中,
虚拟表单
现在是模型的对象不,虚拟表单仍然没有。我不明白为什么CreateView需要一个模型。不过,使用表单上下文变量可以很好地工作。