Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.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
Python 用Django生成复杂表_Python_Django_Django Templates - Fatal编程技术网

Python 用Django生成复杂表

Python 用Django生成复杂表,python,django,django-templates,Python,Django,Django Templates,虽然使用django生成简单表非常容易,但我似乎不知道如何生成复杂表 当前,我的表是用 <table class="table-striped table"> {% for row in table %} <tr> {% for item in row %} <td>{{ item }}</td> {% endfor %} </tr> {% endf

虽然使用django生成简单表非常容易,但我似乎不知道如何生成复杂表

当前,我的表是用

<table class="table-striped table">
    {% for row in table %}
    <tr>
        {% for item in row %}
        <td>{{ item }}</td>
            {% endfor %}
    </tr>
    {% endfor %}
</table>
对象是需要特定解析的对象

我需要列表在内部可循环,而不是将其格式化为列表表示法,而不改变其他对象的外观。

您可以创建一个值并呈现字符串,或者如果它是列表,则呈现列表中的每个项目

# custom_filters.py
@register.filter(name='string_or_list')
def string_or_list(value, delimiter='\n'):
    """Renders string or each instance of a list with given delimiter."""
    if isinstance(value, list):
        return delimiter.join(value)
    return value
然后在模板中执行以下操作:

{% load custom_filters %}
<table class="table-striped table">
    {% for row in table %}
    <tr>
        {% for item in row %}
            <td>{{ item|string_or_list }}</td>
        {% endfor %}
    </tr>
    {% endfor %}
</table>
{%load custom_filters%}
{表%中的行为%1}
{第%行中的项目为%1}
{{item}字符串{u或{u列表}
{%endfor%}
{%endfor%}

你能详细说明一下这个问题吗?为什么不能添加另一个
for
循环?当运行另一个for循环时,它将迭代常规字符串。此外,对象将需要非常特定的格式,因此我需要某种类型的类型验证。我在Django没有看到任何能做到这一点的东西。这个问题相当模糊。请分享您使用过的视图和模型。
{% load custom_filters %}
<table class="table-striped table">
    {% for row in table %}
    <tr>
        {% for item in row %}
            <td>{{ item|string_or_list }}</td>
        {% endfor %}
    </tr>
    {% endfor %}
</table>