django - 模板不显示 html 内容
问题描述
/deals/amp-stories 处的 TemplateSyntaxError 第 576 行的块标记无效:“else”,预期为“empty”或“endfor”。您是否忘记注册或加载此标签?
请求方式:GET请求
网址: http://localhost:8001/deals/amp-stories?card_ids=1 Django
版本:2.2.7 异常类型:TemplateSyntaxError 异常值:
第 576 行的块标记无效:“else”,预期为“empty”或“endfor”。您是否忘记注册或加载此标签?
我正在尝试创建一个将在 django 中加载模板视图的视图,但出现上述错误。
<body>
<!-- supports-landscape -->
{% if card_list %}
<amp-story standalone title="Best islands to visit from Guernsey" publisher="add here" publisher-logo-src="add here" poster-portrait-src="add-logo" poster-square-src="add-logo-here" poster-landscape-src="add-logo-here" supports-landscape>
{% for card in card_list %}
<amp-story-page id="slide_3" class="i-amphtml-element i-amphtml-layout-container i-amphtml-layout i-amphtml-story-page-loaded" i-amphtml-layout="container" aria-label="null" distance="0" i-amphtml-visited="" active="">
<amp-story-grid-layer template="fill" class="zoomIn i-amphtml-element i-amphtml-layout-container i-amphtml-story-layer i-amphtml-story-grid-template-fill i-amphtml-layout" i-amphtml-layout="container">
<amp-img src="{{card.card_image}}" width="1080" height="1920" class="i-amphtml-element i-amphtml-layout-fixed i-amphtml-layout-size-defined i-amphtml-layout" i-amphtml-layout="fixed" style="width: 1080px; height: 1920px; --loader-delay-offset:6ms !important;" i-amphtml-auto-lightbox-visited="">
<img decoding="async" src="{{card.card_image}}" class="i-amphtml-fill-content i-amphtml-replaced-content">
</amp-img>
</amp-story-grid-layer>
<div class="i-amphtml-story-spinner" aria-hidden="true" aria-label="Loading video">
<div class="i-amphtml-story-spinner-container">
<div class="i-amphtml-story-spinner-layer">
<div class="i-amphtml-story-spinner-circle-clipper left"></div>
<div class="i-amphtml-story-spinner-circle-clipper right"></div>
</div>
</div>
</div>
</amp-story-page>
(% endfor %)
{% else %}
<p>There are no cards in the collection.</p>
{% endif %}
</amp-story>
</body>
</html>
视图.py
class StoryList(ListView):
model = Card
context_object_name = "all_cards"
template_name = "card_list.html"
网址.py
urlpatterns = [
path('amp-stories', StoryList.as_view(), name='ampstories'),
]
模型.py
class Card(models.Model):
tag = models.ManyToManyField(CardTag)
card_name = models.CharField(max_length=200)
card_image =models.ImageField(upload_to='slides')
card_createdate = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['card_name']
def __str__(self):
return self.card_name
def slide_thumbnail(self):
return html.format_html(u'<img src="'+settings.HOSTNAME+'%s" width="100px"/>' % (self.card_image.url))
slide_thumbnail.short_description = 'Thumbnail'
解决方案
在您的模板中,您编写而不是(% endfor %)
{% endfor %}
. 通过使用圆括号 ( (% … %)
) 而不是花括号 ( {% … %}
),模板引擎不会将其视为模板标签,因此您仍然在{% for … %}
标签中。
因此,您应该重写您的模板以使用{% endfor %}
而不是.(% endfor %)
推荐阅读
- java -
导入项目时在 .classpath 文件中被覆盖 - javascript - 消除数组中较小的值
- c - 代码在达到交换功能后停止,如果我将其注释掉,它会完美运行
- c - 关于 C 语言文件的 rewind() 函数的问题
- c++ - 不理解以下关于编译器可能对普通 {} 定义执行 `memcpy` 优化的评论
- java - 回调函数作为Java中的参数
- angular - 我无法通过 DTO 模式组织数据并将数据发送到 angular2 上的服务
- javascript - 如何使用电话号码登录设置 React 和 Firebase 身份验证?
- numbers - 最佳聚类数 - FUNcluster(x, i, ...) 中的错误:聚类中心多于不同的数据点
- javascript - 如何删除字符串中的 (.),然后用点显示每个字母字符