Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 在不同的表中显示表单字段_Django_Templates_Field - Fatal编程技术网

Django 在不同的表中显示表单字段

Django 在不同的表中显示表单字段,django,templates,field,Django,Templates,Field,在我的模板中,我希望在不同的表中显示带有for循环的模型的所有字段。每个表都有不同数量的字段。我只知道字段的顺序 我的模型: class Act(models.Model): #table_1 my_field=models.IntegerField(max_length=4, blank=False, null=False) bar=models.IntegerField(max_length=2, blank=False, null=False) ...

在我的模板中,我希望在不同的表中显示带有for循环的模型的所有字段。每个表都有不同数量的字段。我只知道字段的顺序

我的模型:

class Act(models.Model):
    #table_1
    my_field=models.IntegerField(max_length=4, blank=False, null=False)
    bar=models.IntegerField(max_length=2, blank=False, null=False)
    ...

    #table_2
    test=models.CharField(max_length=2, blank=True, null=True, default=None)
    my_new_field=models.CharField(max_length=2, blank=True, null=True, default=None)
    blabla=models.CharField(max_length=2, blank=True, null=True, default=None)
    ...

    #table_3
    foo=models.IntegerField(max_length=2, blank=False, null=False)
    ...
我的模板:

<table id="table_1">
    <!-- display my_field and bar -->
    {% for field in form %}
    {% endfor %}
</table>

<table id="table_2">
    <!-- display test, my_new_field and blabla -->
    {% for field in form %}
    {% endfor %}
</table>

<table id="table_3">
    <!-- display foo -->
    {% for field in form %}
    {% endfor %}
</table>

{%形式的字段为%}
{%endfor%}
{%形式的字段为%}
{%endfor%}
{%形式的字段为%}
{%endfor%}

有可能吗?

多亏了Anto Vinish,我找到了一个可以接受的解决方案:使用
切片

<table id="table_1">
    <!-- display my_field and bar -->
    {% for field in form|slice:":10" %}
    {% endfor %}
</table>

<table id="table_2">
    <!-- display test, my_new_field and blabla -->
    {% for field in form|slice:"10:25" %}
    {% endfor %}
</table>

<table id="table_3">
    <!-- display foo -->
    {% for field in form|slice:"25:" %}
    {% endfor %}
</table>

{%用于格式为| slice::10%%的字段
{%endfor%}
{形式为| slice:“10:25”%%的字段的百分比}
{%endfor%}
{表格| slice:“25:%”中的字段为%
{%endfor%}
如果在模型中添加字段,则必须正确计数并更新数字,但至少没有其他需要更改的内容。没有额外的模型,没有额外的代码,所以没有额外的查询


解决了

您可以使用form.fieldname和form.fieldname,每次手动输入它如果有数百个字段怎么办?您要在模板中使用数百个字段吗?还有其他方法可以使用三个不同的模型通过外键连接第一个表:10个字段,第二个表:15,第三个表:35。创建不同的模型是一个好的/可接受的解决方案吗?如果您知道计数,您也可以使用slice选项….,{%用于表单| slice::10“%}中的字段,它最多会显示10个字段,即使这不是您问题的最佳解决方案,但它是完成工作的提示