2013-06-11 51 views
0

我很難爲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): 
     context = super(generic.CreateView, self).get_context_data(**kwargs) 
     print context 

我想設置context_object_name = 'fictive_form'將改變上下文對象的名稱。 原來它只是給出了一個空對象:

{'fictive_form': None, 'form': <fictive.forms.FictiveForm object at 0x7f925807a9d0>, u'view': <fictive.views.FictiveCreateView object at 0x7f925807aa50>} 

我失去的是什麼?

回答

0

context_object_name是有你的模型

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 

在你的模板,fictive_form現在是YourModel

的對象
+0

不,fictive_form仍然是無。我不明白爲什麼CreateView會需要模型。使用表單上下文變量工作正常。 – Adrien

相關問題