How to render a form part of dictionary

I have a view in which I build a dictionnary containing forms:

        businesses = Business.objects.filter(customer=company)
        framework_agreement_forms_dic = {}
        for business in businesses:
            prefix = f'affaire_{business.id}'
            framework_agreement_forms_dic[business.id] = Framework_agreementForm(auto_id=f'id_{prefix}_%s', instance=business)
        context['framework_agreement_forms'] = framework_agreement_forms_dic

I don’t know how to render the form corresponding to a key which is form.instance.id.

Here is what I have in my template:

          {% for form in business_formset %}
            <div class="row mx-2">
              <div class="col-md-auto ">
                <td style = "display:none">{{ form.id }}</td>
                {% if form.instance.id %}
                <a href="/salesforecast/businesses/{{form.instance.id}}" title="Gérer les échéances prévisionnelles de facturation"><i class='bx bxs-detail nav_icon'></i></a>
                {% endif %}
              </div>
              <div class="col">
                {{form.name}}
              </div>
              <div class="col-md-auto">
                <label for="{{form.account_exec.id_for_label}}">Responsable</label>
                {{form.account_exec}}
              </div>
            </div>
            <div class="row mx-2">
              <div class="col-md-3">
                <label for="{{form.framework_agreement.id_for_label}}">Accord-cadre</label>
                {{form.framework_agreement}}
                <span class="btn btn-link" data-bs-toggle="modal" data-bs-target="#AddFrameworkAgreement{{form.instance.id}}">
                  <i class='bx bxs-plus-circle nav_icon' title="Ajouter un accord-cadre et y associer cette affaire"></i>
                </span>
                
                <!-- Modal d'ajout d'un accord-cadre-->
                <div class="modal fade" id="AddFrameworkAgreement{{form.instance.id}}" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel">
                  <div class="modal-dialog">
                    <div class="modal-content">
                      <div class="modal-header">
                        <h5 class="modal-title" id="AddFrameworkAgreementLabel">Ajout d'un accord-cadre</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                      </div>
                      <div class="modal-body">
                        {% with fa_form=framework_agreement_forms[form.instance.id] %}
                            {{ fa_form.as_table }}
                        {% endwith %}
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                        <button type="submit" formmethod="POST"  name="add_framework_agreement{{form.instance.id}}" 
                            id="add_framework_agreement{{form.instance.id}}"  class="btn btn-primary">
                            <i class='bx bx-save nav_icon'></i>Confirmer
                        </button>
                      </div>
                    </div>
                  </div>
                </div>               
              </div>
              <div class="col-md-3">
                <label for="{{form.po_market.id_for_label}}">Marché à BC</label>
                {{form.po_market}}
              </div>
            </div>
            <div class="row mx-2"> 
              <div class="col-md-3">
                <label for="{{form.ponderation.id_for_label}}">{{form.ponderation.label}}</label>          
                {{ form.ponderation }}
              </div>
              <div class="col-md-3 ">
                <label for="{{form.amount.id_for_label}}">{{form.amount.label}}</label>          
                {{ form.amount }}
              </div>
              <div class="col-md-3">
                <label for="{{form.amount_pondered.id_for_label}}">{{form.amount_pondered.label}}</label>          
                {{ form.amount_pondered }}
              </div>
              <div class="col-md-3">
                <label for="{{form.amount_to_plan.id_for_label}}">{{form.amount_to_plan.label}}</label>          
                {{ form.amount_to_plan }}
              </div>  
            </div>    
            <div class="line"></div>
          {% endfor %}

But the part

                        {% with fa_form=framework_agreement_forms[form.instance.id] %}
                            {{ fa_form.as_table }}
                        {% endwith %}

generates an error: `Could not parse the remainder: ‘[form.instance.id]’ from framework_agreement_forms[form.instance.id]’

Any help is welcome

My first guess would be to try:

{% with inst_id=form.instance.id %}
    {% with fa_form=framework_agreement_forms.inst_id %}
        {{ fa_form.as_table }}
    {% endwith %}
{% endwith %}

(Just winging this, I’m not in a position to try this out.)

Also, see the Note box at The Django template language | Django documentation | Django

Hi Ken,

I tried, but it does not work…

I’ve found my way with a custom tag

{{ framework_agreement_forms|get_item:form.instance.id }}

where

from django import template

register = template.Library()

# Permet, dans un template, de sélectionner une valeur dans le dictionnaire par sa clé. 
# Utilisé notamment dans Company_update_form pour afficher les formulaires d'accord-cadre
@register.filter(name='get_item')
def get_item(dictionary, key):
    return dictionary.get(key)

in myapp/templatetags/custom_filters.py (along wiht an empty __init__py)

Thanks