首页 > 解决方案 > 模板不显示 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'

标签: django

解决方案


在您的模板中,您编写(% endfor %)而不是{% endfor %}. 通过使用圆括号 ( (% … %)) 而不是花括号 ( {% … %}),模板引擎不会将其视为模板标签,因此您仍然在{% for … %}标签中。

因此,您应该重写您的模板以使用{% endfor %}而不是(% endfor %).


推荐阅读