0

I'm having a hard time setting the context object names for django's generic class based views.

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

I thought setting context_object_name = 'fictive_form' would change the name of the context object. Turns out it just gives an empty object :

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

What am i missing here ?

4

1 回答 1

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 的一个对象

于 2013-06-11T14:47:56.657 回答