4

如何使用 formset 和 extra=3 创建此表?

桌子:

| my name first column | second column | third column | fourth column |

| formset1.field1.label | formset1.field1 | formset2.field1 | formset3.field1 |

| formset1.field2.label | formset1.field2 | formset2.field2 | formset3.field2 |

| formset1.field3.label | formset1.field3 | formset2.field3 | formset3.field3 |

| formset1.field4.label | formset1.field4 | formset2.field4 | formset3.field4 |

在视图中,我创建了标准表单集:

def my_view(request, id_object):
    getobject = get_object_or_404(Model, id=id_object)
    FormSetInit = inlineformset_factory(Model, Model2, form=FormModel2, extra=3)
    FormSet = FormSetInit(instance=getobject, prefix='model2')
    response = {}
    response['Formset'] = FormSet
    return render_to_response('my_template.html', response)

在模板'my_template.html'中我尝试使用这个:

<table>
{% for form in Formset %}
  {% if forloop.counter == 1 %}
    {% for field in form %}
      {% if forloop.counter < 8 %}
        <tr>
          <td class="first">{{ field.label }}</td>
          <td>{{ field }}</td>
          <td>{{ field }}</td>
          <td class="last">{{ field }}</td>
          {% if forloop.counter !=  7 %}
            </tr>
          {% endif %}
      {% else %}
        {{ field }}
        {% if forloop.last %} </tr> {% endif %}
      {% endif %}
    {% endfor %}
  {% endif %}
{% endfor %}
</table>

但这不是一个好主意..:/

4

3 回答 3

5

我也遇到了麻烦,但在https://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs#displaying-a-form-using-a的帮助下设法让它工作-模板

无论如何,这是您可以尝试的模板,它基本上遍历每个表单:

<table>
    <thead>
        {% for form in Formset.forms %}
            {% if forloop.first %}
                {% for field in form %}
                    <th>{{ field.label_tag }}</th>
                {% endfor %}
            {% endif %}
    </thead>
    <tbody>
            <tr>
                {% for field in form %}
                    <td>{{ field }}</td>
                {% endfor %}
            </tr>
        {% endfor %}
    </tbody>
</table>

希望有帮助!不确定这是否是最好的方法,但它确实有效!

于 2013-07-06T15:05:13.490 回答
2
<table id='supertable' border=1>
<tr>
{% for form in formset %}
    {% if forloop.counter = 1 %}    
    <td>
        <table>
        {% for field in form.visible_fields %}
            <tr><td style="background: cyan; line-height:1; white-space:nowrap;" >{{ field.label_tag }}</td></tr>
        {% endfor %} 
        </table>
    </td>
    {% endif %}
    <td>
        <table>
        {% for field in form.visible_fields %}
            <tr><td style="background: pink; line-height:1; white-space:nowrap;" >{{ field}}</td></tr>
        {% endfor %} 
        </table>
    </td>
{% endfor %} 
</tr>
</table>
于 2014-02-04T20:41:16.907 回答
2

在我试图实现同样的目标并最终以脆的形式实现之后,提供了一个更新的答案

形式:

from crispy_forms.helper import FormHelper, Layout
...

class MyForm(forms.ModelForm):

    class Meta:
        model = MyModel
        fields = ['field1', 'field2', 'field3']


MyFormSet = modelformset_factory(MyModel, form=MyForm, extra=0)


class MyFormSetHelper(FormHelper):
    def __init__(self, *args, **kwargs):
        super(MyFormSetHelper, self).__init__(*args, **kwargs)
        self.layout = Layout(
            'field1',
            'field2',
            'field3'
        )
        self.template = 'bootstrap/table_inline_formset.html'

意见:

formset = MyFormSet(queryset=my_qs)
helper = MyFormSetHelper()
context = {'formset': formset, 'helper': helper}
return render(request, 'my_template.html', context)

模板:

{% extends "base.html" %}
{% load crispy_forms_tags %}

{% block content %}

<form action="" method="post">
    {% csrf_token %}
    {% crispy formset helper %}
</form>

{% endblock content %}
于 2018-02-21T16:34:50.157 回答