Django与模型formset形成字段问题

时间:2015-03-20 12:35:35

标签: python django django-forms django-templates

我在模板中渲染单个表单字段时遇到问题。我有一个模型formset,我在创建后重新排序,使模板上的显示更容易一些。没有什么太复杂,但渲染表单字段是不行的。您可以在模板中看到我尝试渲染{{ form.train }},但输出中没有任何内容。但是,表单肯定存在,因为{{ form.instance.user.name }}正常工作。

我打开了PDB并检查了我添加到字典中的表单变量,它显示<django.forms.widgets.ScheduleForm object at 0x10c58bc50>。我不确定这是否有帮助,但我想提供尽可能多的信息。

模型表格

class ScheduleForm(ModelForm):

    class Meta:
        model = models.Schedule
        fields = [
            'train',
            'semi',
            'tri_axle',
            'flow_boy',
            'misc',
            'material',
            'notes'
        ]

views.py

formset_fields = ('train','semi','tri_axle','flow_boy','misc','material','notes')
ScheduleFormSet = modelformset_factory(models.Schedule, fields=formset_fields, extra=0)

formset = ScheduleFormSet(queryset=queryset)

# Getting form in the right format
ordered_forms = {}
for form in formset:
    # Make sure the job exists on the object
    if not form.instance.job.number in ordered_forms:
        ordered_forms[form.instance.job.number] = {}

    # Make sure the user exists on the object
    if not form.instance.user.name in ordered_forms[form.instance.job.number]:
        ordered_forms[form.instance.job.number][form.instance.user.name] = []

    # Append to correct place.
    ordered_forms[form.instance.job.number][form.instance.user.name].append(form)

# Dict will look like
# { 'jobID' : { 'user' : [form1,form2,form3] } }

模板

    {% for job, users in ordered_forms.items %}
        <h2>{{ job }}</h2>
        {% for user, forms in users %}
            <table class='table striped'>
                <thead>
                    <tr>
                        <th>{{ user }}</th>
                        <th>Train</th>
                        <th>Semi</th>
                        <th>Tri-Axle</th>
                        <th>Flow Boy</th>
                        <th>Misc</th>
                        <th>Material</th>
                        <th>Notes</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    {% for form in forms %}
                    <tr>
                        <td>{{ form.instance.broker.name }}</td>
                        <td>{{ form.train }}</td>
                        <td>Semi</td>
                        <td>Tri-Axle</td>
                        <td>Flow Boy</td>
                        <td>Misc</td>
                        <td>Material</td>
                        <td>Notes</td>
                        <td></td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        {% endfor %}
    {% endfor %}

1 个答案:

答案 0 :(得分:0)

原来我忘记将.items添加到{% for user, forms in users %}

{% for job, users in ordered_forms.items %}
        <h2>{{ job }}</h2>
        {% for user, forms in users.items %}
            <table class='table striped'>
                ....
        {% endfor %}
    {% endfor %}