Alejandro Veintimilla
Alejandro Veintimilla

Reputation: 11543

Django CreateView not sending form

This view is not sending the form. I don't know why. I can see it is not sending the form cause I'm printing the context at the end of the get_context_data function.

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

Upvotes: 1

Views: 379

Answers (1)

Shang Wang
Shang Wang

Reputation: 25549

As I put in the comment, you forgot to pass *args and **kwargs to the parent class when you call super, so it should be:

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

*args and **kwargs are the parameters defined by django get_context_data and they are definitely used inside django. If you don't pass them to the parent class, django is lack of certain information that's needed. Without them django couldn't construct the form thus your context doesn't have any form.

Upvotes: 1

Related Questions