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是否完全使用這些參數,我可能會在稍後訪問某些內容時更新我的答案。 –