【问题标题】:Django adding spaces and quotation marks when pushing string to templateDjango在将字符串推送到模板时添加空格和引号
【发布时间】:2017-01-28 17:51:42
【问题描述】:

我尝试在视图中构建一个表并将其推送到该视图的模板中,因为在模板中执行此操作被证明是不可行的。我可以生成一个包含正确信息的字符串,但是当在浏览器中评估该模板变量时,有额外的引号和空格似乎使表格无法正确生成。

这是视图中的代码:

    for unique_activity in uniquelist:
        currentrow = '<tr data-activityID="' + str(unique_activity[0])+ '">' + '<td>' + str(unique_activity[1]) + '</td>'
    for visit in visits_list:
        for activity in visit.activity_set.all():
            if activity.id == unique_activity[0]:
                currentrow = currentrow + '<td>' + 'Reps:'+ str(activity.Repetitions) + '</td>'
            else:
                noact = True
        if noact == True:
            currentrow = currentrow + '<td></td>'
    currentrow = currentrow + '</tr>\n'
    tablerows.append(currentrow)

    table = '<table>'
    for row in tablerows:
        table = table + row
    table = table + '</table>'
    table = str(table)

输出就是它所需要的......一个例子&lt;table&gt;&lt;tr data-activityID="1"&gt;&lt;td&gt;Initial Evaluation&lt;/td&gt;&lt;td&gt;Reps:None&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr data-activityID="3"&gt;&lt;td&gt;Cold Pack&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;Reps:None&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr data-activityID="6"&gt;&lt;td&gt;Recumbent Exercise Bike&lt;/td&gt;&lt;td&gt;Reps:12&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr data-activityID="7"&gt;&lt;td&gt;Leg Press&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr data-activityID="8"&gt;&lt;td&gt;Shoulder Ladder&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt;

但是,这就是 DOM 中显示的内容,我输出到模板的所有内容只是 {{ table }},这就是输出到 DOM 信息中的内容,

只显示字符串,而不是表格。

这里是模板sn-p

    <body>
{% if activity_list %}
    <table>
    {% for activity in activity_list %}
        <tr>
            <td>{{ activity.ActivityType.Name }}</td>
        </tr>
    {% endfor %}
    </table>
{% endif %}
{{ table }}
</body>

我不知道发生了什么......

【问题讨论】:

  • 您能否提供一个 sn-p 显示您在模板中包含 {{ table }}
  • 完成,感谢您的评论。它是死香草,虽然......有一个差异。上面的表格。

标签: django django-templates django-views


【解决方案1】:

这听起来像是您的基本模板(或其他地方)中可能有autoescape。如果您希望您的变量以 HTML 格式包含,而不是安全地转义为文本,您必须关闭它周围的autoescape,或者用safe template filter 标记它。这是一个示例模板来尝试说明。

<p>The following table should be rendered as escaped safe text.</p>
{{ table }}

<p>But with the safe filter, it will be rendered as html!</p>
{{ table|safe }}

<p>And maybe, I want to do this to a few things or have a block of safe html, so I'll just turn the autoescape off for a bit!</p>
{% autoescape off %}
{{ table }}
{{ some_other_safe_html }}
{% endautoescape %}

使用您提供的 sn-p,这是带有安全转义的代码:

<body>
{% if activity_list %}
    <table>
    {% for activity in activity_list %}
        <tr>
            <td>{{ activity.ActivityType.Name }}</td>
        </tr>
    {% endfor %}
    </table>
{% endif %}
{{ table|safe }}
</body>

【讨论】:

  • 谢谢。从来不知道这是一种选择。感谢您快速准确的回复。
  • @Gradatc 没问题!很高兴我能帮上忙。
猜你喜欢
  • 2019-08-03
  • 2013-03-09
  • 1970-01-01
  • 2023-02-04
  • 1970-01-01
  • 2016-10-15
  • 2022-09-30
  • 1970-01-01
  • 2014-09-24
相关资源
最近更新 更多