GrantU
GrantU

Reputation: 6545

Django How to FormView rename context object?

I have a class view which uses FormView. I need to change the name of the form i.e. this is what it used to be in my old function view:

 upload_form = ContactUploadForm(request.user)
 context = {'upload': upload_form,}

With my new view I'm assuming I can rename using the get_context_data method but unsure how.

How can I rename this form to upload instead of form as my templates uses {{ upload }} not {{ form }}? Thanks.

Current Class View:

class ImportFromFile(FormView):

    template_name = 'contacts/import_file.html'
    form_class = ContactUploadForm

    def get_context_data(self, **kwargs):
        """
        Get the context for this view.
        """
        # Call the base implementation first to get a context.
        context = super(ImportFromFile, self).get_context_data(**kwargs)

        return context

Upvotes: 4

Views: 3561

Answers (3)

MagicLAMP
MagicLAMP

Reputation: 1072

I am using Django==4.2. The Django 2.0+ answer is for ListView, not FormView, but you can do the same thing with your own FormView class. EG (sorry about the incorrect python indentation below);

from django.views.generic import FormView
class FormViewIntro(FormView):

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    if hasattr(self, 'context_object_name'):
        context[self.context_object_name] = context.pop('form')
    return context

which you would use in the same way as ListView with context_object_name. EG;

from somewhere.myclasses import FormViewIntro
class NomadsInsurance(FormViewIntro):

    template_name = 'aftermarket/nomads_insurance_form.html'
    form_class = NomadsInsuranceForm
    context_object_name = 'insurance_form'

Then in the template, you could refer to insurance_form rather than "form"

Upvotes: 0

Jon
Jon

Reputation: 2557

Django 2.0+ provides support for changing context object names. See: Built-in class-based generic views

Making “friendly” template contexts

You might have noticed that our sample publisher list template stores all the publishers in a variable named object_list. While this works just fine, it isn’t all that “friendly” to template authors: they have to “just know” that they’re dealing with publishers here.

Well, if you’re dealing with a model object, this is already done for you. When you are dealing with an object or queryset, Django is able to populate the context using the lowercased version of the model class’ name. This is provided in addition to the default object_list entry, but contains exactly the same data, i.e. publisher_list.

If this still isn’t a good match, you can manually set the name of the context variable. The context_object_name attribute on a generic view specifies the context variable to use:

class PublisherList(ListView):
    model = Publisher
    context_object_name = 'choose the name you want here'

Upvotes: 4

Andrei Kaigorodov
Andrei Kaigorodov

Reputation: 2165

Try this:

class ImportFromFile(FormView):

    template_name = 'contacts/import_file.html'
    form_class = ContactUploadForm

    def get_context_data(self, **kwargs):
        """
        Get the context for this view.
        """
        kwargs['upload'] = kwargs.pop('form')
        return super(ImportFromFile, self).get_context_data(**kwargs)

Upvotes: 10

Related Questions