1
此视图不发送表单。我不知道为什么。我可以看到它没有发送表单,因为我在get_context_data
函数的末尾打印了context
。Django CreateView不发送表格
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
当你调用'super'时,你不应该将'* args'和'* kwargs'传递给父类吗? 'context = super(CrearFeralSpirit,self).get_context_data(* args,** kwargs)' –
真的,谢谢,那有效。如果你想要你可以写出答案,我会接受它。另外....我不明白它为什么会起作用,当我调用'super'时,它调用没有'args'和'kwargs'的'get_context_data',但为什么它需要'args'和'kwargs “首先呢?是不是已经存在的“形式”?在什么时候,'form'被添加到'context'中......我很困惑。 :P – alejoss
我更新了答案。我无法检查django是否完全使用这些参数,我可能会在稍后访问某些内容时更新我的答案。 –