TransWikia.com

Django Formset Initialization in POST and GET

Stack Overflow Asked by aAnnAa on December 11, 2021

I want a formset initialized with a varying number of forms.

class MapView(TemplateView):

def get(self, request, *args, **kwargs):
    template_name = 'terms/termmappingnames.html'

    formlength = request.session["formlength"]

    FieldMappingFormset = formset_factory(FieldMappingForm, extra=formlength)
    formset = FieldMappingFormset()

    choices = [(x, x) for x in ["a", "b", "c"]
    for i, form in enumerate(formset):
        form.fields["map_to_option"].choices = choices
        form.fields["map_to_option"].label = metadata_names[i]

    context = {
        "formset": formset
    }
    return TemplateResponse(request, template_name, context)

def post(self, request, *args, **kwargs):
    template_name = 'terms/termmappingvalues.html'  
    # error here
    formset = FieldMappingFormSet(request.POST)


    context = {
        
    }
    return TemplateResponse(request, template_name, context)

This is my TemplateView and I dont know how to access the FieldMappingFormset created in the get method. Diplaying the form with "formlength" number of forms works, however I can’t access it in POST because, obviously, FieldMappingFormSet is not defined. It doesn’t work using class variables or global variables.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP