Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/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 forms django-在模板中生成重复值/错误代码的更好方法?_Django Forms_Django Templates - Fatal编程技术网

Django forms django-在模板中生成重复值/错误代码的更好方法?

Django forms django-在模板中生成重复值/错误代码的更好方法?,django-forms,django-templates,Django Forms,Django Templates,还在学习django和python 我有一个有很多字段的表单。我想把它们排列成每行两个字段,并使用一个表来实现这一点。我不能简单地遍历所有字段,因为我希望对某些事情有更好的控制 从本质上讲,我将输出一行两个字段,然后如果其中任何字段中存在错误,则生成包含错误的第二行 在模板中,我一遍又一遍地重复以下模式: <tr> <td class="labels">Order Number:</td><td class="textentry">{{

还在学习django和python

我有一个有很多字段的表单。我想把它们排列成每行两个字段,并使用一个表来实现这一点。我不能简单地遍历所有字段,因为我希望对某些事情有更好的控制

从本质上讲,我将输出一行两个字段,然后如果其中任何字段中存在错误,则生成包含错误的第二行

在模板中,我一遍又一遍地重复以下模式:

 <tr>
     <td class="labels">Order Number:</td><td class="textentry">{{ order.ordernum }}</td>
     <td class="labels">Order Status:</td><td class="textentry">{{ order.status }} </td>
 </tr>
     {% if order.ordernum.errors %}
        {% for error in order.ordernum.errors %}
            <tr class="errors"><td colspan=2> {{ error|escape }}</td>
        {% endfor %}
        {% if order.status.errors %}
           {% for error in order.status.errors %}
              <td colspan=2> {{ error|escape }}</td>
           {% endfor %}
        {% endif %}
        </tr>
     {% endif %}

订单号:{Order.ordernum}
订单状态:{Order.Status}
{%if order.ordernum.errors%}
{order.ordernum.errors%中的错误为%0}
{{error | escape}
{%endfor%}
{%if order.status.errors%}
{order.status.errors%中的错误为%0}
{{error | escape}
{%endfor%}
{%endif%}
{%endif%}
其中,从重复模式到重复模式的所有变化都是特定的字段名

这种重复让我怀疑:在模板中有更好的方法吗?是否有某种方法可以从模板中调用函数,在模板中我可以只传入字段名

谢谢你的帮助


W.

对于任何感兴趣的人,我不知道是否有更好的解决方案,但模板宏a la djangosippets.org/snippets/363是我解决这个问题的方法。

我想这是否是一个使用模板宏a la的好地方?我想你除了迭代每个字段之外没有其他选择,您希望有多精细的控件?例如,在某些情况下,一行需要两个字段,但在其他情况下,一行需要三行。我使用了te模板宏片段,它几乎完全符合我的需要。。。