Proper way to handle multiple forms on one page in Django

后端 未结 10 2243
既然无缘
既然无缘 2020-11-22 09:19

I have a template page expecting two forms. If I just use one form, things are fine as in this typical example:

if request.method == \'POST\':
    form = Au         


        
10条回答
  •  我在风中等你
    2020-11-22 09:30

    This is a bit late, but this is the best solution I found. You make a look-up dictionary for the form name and its class, you also have to add an attribute to identify the form, and in your views you have to add it as a hidden field, with the form.formlabel.

    # form holder
    form_holder = {
        'majeur': {
            'class': FormClass1,
        },
        'majsoft': {
            'class': FormClass2,
        },
        'tiers1': {
            'class': FormClass3,
        },
        'tiers2': {
            'class': FormClass4,
        },
        'tiers3': {
            'class': FormClass5,
        },
        'tiers4': {
            'class': FormClass6,
        },
    }
    
    for key in form_holder.keys():
        # If the key is the same as the formlabel, we should use the posted data
        if request.POST.get('formlabel', None) == key:
            # Get the form and initate it with the sent data
            form = form_holder.get(key).get('class')(
                data=request.POST
            )
    
            # Validate the form
            if form.is_valid():
                # Correct data entries
                messages.info(request, _(u"Configuration validée."))
    
                if form.save():
                    # Save succeeded
                    messages.success(
                        request,
                        _(u"Données enregistrées avec succès.")
                    )
                else:
                    # Save failed
                    messages.warning(
                        request,
                        _(u"Un problème est survenu pendant l'enregistrement "
                          u"des données, merci de réessayer plus tard.")
                    )
            else:
                # Form is not valid, show feedback to the user
                messages.error(
                    request,
                    _(u"Merci de corriger les erreurs suivantes.")
                )
        else:
            # Just initiate the form without data
            form = form_holder.get(key).get('class')(key)()
    
        # Add the attribute for the name
        setattr(form, 'formlabel', key)
    
        # Append it to the tempalte variable that will hold all the forms
        forms.append(form)
    

    I hope this will help in the future.

提交回复
热议问题