ModelForm 组。 Ne pas afficher le form.id empêche la validation du formulaire [关闭]

问题描述 投票:0回答:0

J'utilise un ModelForm groupé pour faire un CRUD sous la forme d'une table de données.

Pour que la validation du formulaire se fasse, je suis obligé de faire apparaître le form.id dans une colonne de ma table, comme ci-dessous:

        {% for form in data_form_set %}
        <tr>
            <td>{{ form.id }}</td>
            <td>{{ form.DELETE }}</td>
            <td>{{ form.champ1 }}</td>
            <td>{{ form.champ2}}</td>                  
          </tr>
        {% endfor %}

Quand je sors {{ form.id }} des tags , comme ci-dessous, j'ai systématiquement une erreur de validation

    <table class="table table-hover table-striped"
    data-toggle="table"
    data-pagination="true">
    <thead>
      <tr>
        <th>id</th>
        <th><i class='bx bx-trash nav_icon'></i></th>
        <th>Champ 1</th>
        <th>Champ 2</th>
      </tr>
    </thead>
    <tbody>

        {% for form in data_form_set %}
        <tr>
            {{ form.id }}
            <td>{{ form.DELETE }}</td>
            <td>{{ form.champ1 }}</td>
            <td>{{ form.champ2}}</td>                  
          </tr>
        {% endfor %}
    </tbody>
  </table>

Avec le code ci-dessous, cela fonctionne (mais j'ai une colonne "Id" qui apparait inutilement dans mon tableau (je ne comprends d'ailleurs pas pourquoi Django cache la valeur (hidden)

    {{ data_form_set.management_form }}
<br>
<br>
    <table class="table table-hover table-striped"
    data-toggle="table"
    data-pagination="true">
    <thead>
      <tr>
        <th>id</th>
        <th><i class='bx bx-trash nav_icon'></i></th>
        <th>Champ 1</th>
        <th>Champ 2</th>
      </tr>
    </thead>
    <tbody>

        {% for form in data_form_set %}
        <tr>
            <td>{{ form.id }}</td>
            <td>{{ form.DELETE }}</td>
            <td>{{ form.champ1 }}</td>
            <td>{{ form.champ2}}</td>                  
          </tr>
        {% endfor %}
    </tbody>
  </table>

Avec le code ci-dessous, quand je sors {{ form.id }} des tags , comme ci-dessous, j'ai systématiquement une erreur de validation.

    <table class="table table-hover table-striped"
    data-toggle="table"
    data-pagination="true">
    <thead>
      <tr>
        <th>id</th>
        <th><i class='bx bx-trash nav_icon'></i></th>
        <th>Champ 1</th>
        <th>Champ 2</th>
      </tr>
    </thead>
    <tbody>

        {% for form in data_form_set %}
        <tr>
            {{ form.id }}
            <td>{{ form.DELETE }}</td>
            <td>{{ form.champ1 }}</td>
            <td>{{ form.champ2}}</td>                  
          </tr>
        {% endfor %}
    </tbody>
  </table>
django-forms modelform
© www.soinside.com 2019 - 2024. All rights reserved.