【发布时间】:2017-02-18 23:48:49
【问题描述】:
我有一个模型预订,我在许多模板中使用它。创建自己的HTML/Djangosn-p 很方便,它通过变量/模型方法注入到模板中。
原始的HTML 使用该方法是正确的,但 Django 模板语言没有正确解释。
这是一种预订方式:
def get_html_description(self):
return """<ul>
<li><b>ID:</b> {{ reservation.id }}</li>
<hr>
<li><b>From:</b> {{ reservation.get_text_destination_from }}</li>
<li><b>To:</b> {{ reservation.get_text_destination_to }}</li>
<hr>
<li><b>Date:</b> {{ reservation.get_date }}</li>
<li><b>Time:</b> {{ reservation.get_time }}</li>
</ul>"""
现在我正在尝试将此代码注入到模板中:
<div class="events">
{% for reservation in data.1 %}
<div class="event">
<h4>{{ reservation.get_text_destination_from }} to {{ reservation.get_text_destination_to }}</h4>
<div class="desc">
{% autoescape off %}{{ reservation.get_html_description }}{% endautoescape %}
</div>...
...
不幸的是,它呈现如下内容:
你知道该怎么做吗?我已经尝试过过滤器|safe 和{% autoescape off %}
【问题讨论】:
-
创建inclusion tag 可能比在模型中存储模板更好。
标签: python html django templates django-templates