[Answered ]-How to change the key of the form passed to a template using a class based view

2👍

✅

The error you are getting is because of the way you are using super. The self goes in the super() for Python 2.7, and you don’t need to pass the self at all for Python 3.

Python 2.7:

return super(MyClassView, self).render_to_response(context, **response_kwargs)

Python 3:

return super().render_to_response(context, **response_kwargs)

If you dig into the CreateView you will see that it is in the get() that form actually gets assigned to the form key.

def get(self, request, *args, **kwargs):
    """
    Handles GET requests and instantiates a blank version of the form.
    """
    form_class = self.get_form_class()
    form = self.get_form(form_class)
    return self.render_to_response(self.get_context_data(form=form))

You could add in the key the way you are now, but the form key will still be getting assigned too.

I might suggest overriding the get().

def get(self, request, *args, **kwargs):
    self.object = None
    form_class = self.get_form_class()
    form = self.get_form(form_class)
    return self.render_to_response(self.get_context_data(registeredteamform =form))

Leave a comment