Django FormView does not have form context
Asked Answered
A

1

16

When defining a FormView derived class:

class PrefsView(FormView):
    template_name = "prefs.html"
    form_class = MyForm         # What's wrong with this?
    def get(self,request):
        context = self.get_context_data()
        context['pagetitle'] = 'My special Title'
        context['form'] = MyForm    # Why Do I have to write this?
        return render(self.request,self.template_name,context)

I expected the line context['form'] = MyForm was not needed, since form_class is defined, but without it {{ form }} is not passed to template.
What I'm doing wrong?

Arch answered 30/10, 2013 at 15:41 Comment(0)
T
19

In the context, form should be the instantiated form, not the form class. Defining the form_class is completely separate from including the instantiated form in the context data.

For the example you've given, I think you'd be better to override get_context_data instead of get.

def get_context_data(self, **kwargs):
    context = super(PrefsView, self).get_context_data(**kwargs)
    context['pagetitle'] = 'My special Title'
    return context
Tollgate answered 30/10, 2013 at 15:57 Comment(5)
Yes, this is a good advice, but still does not answer the qeustion, why do I need that line? In several examples I see the use of form_class instead.Arch
In practice, you say defining form_class is completely separate from including the instantiated form in template. So maybe I didn't fully understand all this, for what purpose one should define form_class?Arch
You don't need that line if you don't override get. If you do, then you're preventing the default implementation from including the form, so of course you need to include it yourself.Famed
When using a FormView, form_class defines what type of form to instantiate. The get method instantiates a a form, and includes it in the context. Since you are overriding get, you need to include form in the context as well. Because you seem to be overriding the get method to add an extra variable to the template context, I suggested overriding get_context_data instead - that way you don't need to worry about the other stuff that the get method does.Tollgate
But I was returning super() :( Oh well, I guess it was some of the inherited classes' fault.. Thank you for this!Snapp

© 2022 - 2024 — McMap. All rights reserved.