Django CreateView 不发送表单
Django CreateView not sending form
此视图未发送表单。我不知道为什么。我可以看到它没有发送表单,因为我在 get_context_data
函数末尾打印 context
。
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
传递给父级 class,因此应该是:
context = super(CrearFeralSpirit, self).get_context_data(*args, **kwargs)
*args
和**kwargs
是django定义的参数get_context_data
,肯定是django内部使用的。如果您不将它们传递给父 class,django 将缺少所需的某些信息。没有它们,django 无法构造表单,因此您的上下文没有任何形式。
此视图未发送表单。我不知道为什么。我可以看到它没有发送表单,因为我在 get_context_data
函数末尾打印 context
。
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
传递给父级 class,因此应该是:
context = super(CrearFeralSpirit, self).get_context_data(*args, **kwargs)
*args
和**kwargs
是django定义的参数get_context_data
,肯定是django内部使用的。如果您不将它们传递给父 class,django 将缺少所需的某些信息。没有它们,django 无法构造表单,因此您的上下文没有任何形式。