Django Template:如何在页面上完全转储对象

Pro*_*eus 4 python django django-templates

尽管存在数据,但以下模板未输出任何内容.

我的问题是......我是否可以将"点"对象的内容转储到模板中,这样我才能看到它里面有什么?

template.py

 <h3>{% trans "Points" %}</h3>

    {% if points %}
        <p>{% trans "Total Points" %}: {{ points.Points }}</p>


        <table>
            <thead>
            <tr>
                <th>{% trans "Transaction" %}</th>
                <th>{% trans "Status" %}</th>
                <th>{% trans "Points" %}</th>
            </tr>
            </thead>
            <tbody>
            {% for item in points.Points_items.all %}
                <tr>
                    <td>{{ item.transaction_description }}</td>
                    <td>{{ item.get_status_display }}</td>
                    <td>{{ item.points }}</td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
Run Code Online (Sandbox Code Playgroud)

Joh*_*Mee 6

把它贴在顶部:

<h1>|{{ points }}|</h1>
Run Code Online (Sandbox Code Playgroud)

如果|它之间什么也没有,那么它就是空的.

  • 那没什么?或者其他的东西?如果你想要逐项列出点的内容,可以创建一个`__str ___或`__unicode__`方法. (3认同)

Bry*_*yce 6

我为此使用自定义标签:

# Custom tag for diagnostics
@register.simple_tag()
def debug_object_dump(var):
    return vars(var)
Run Code Online (Sandbox Code Playgroud)

{% load extra_tags %}
...
{% for thing in things %}
  <pre>{% debug_object_dump thing %}</pre>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)