Django CreateView不发送表单

时间:2015-12-26 01:05:45

标签: django django-class-based-views

此视图未发送表单。我不知道为什么。我可以看到它没有发送表单,因为我在context函数的末尾打印get_context_data

class CrearFeralSpirit(CreateView):

    template_name = "hisoka/crear_feral_spirit.html"
    model = FeralSpirit
    fields = ['tipo', 'nombre', 'url']

    def form_valid(self, form):

        fireball = Fireball.objects.get(slug=self.kwargs.get('slug'))
        form.instance.fireball = fireball

        return super(CrearFeralSpirit, self).form_valid(form)

    def get_context_data(self, *args, **kwargs):
        context = super(CrearFeralSpirit, self).get_context_data()
        fireball = Fireball.objects.get(slug=self.kwargs['slug_fireball'])

        context['fireball'] = fireball
        print context  # Here I print the context, no form in it.
        return context

1 个答案:

答案 0 :(得分:1)

正如我在评论中所说,当您致电*args时,您忘记将**kwargssuper传递给父类,因此它应该是:

context = super(CrearFeralSpirit, self).get_context_data(*args, **kwargs)

*args**kwargs是由django get_context_data定义的参数,它们肯定在django中使用。如果你没有将它们传递给父类,那么django缺乏所需的某些信息。没有它们,django无法构建表单,因此您的上下文不具有任何形式。