首页 > 解决方案 > django.template.exceptions.TemplateSyntaxError:第 2 行上的未闭合标签:'block'。寻找其中之一:端块。在 django

问题描述

我正在制作一个 django 应用程序。这是我的index.html模板:

{% extends "blog/base.html" %} 
{% block content %}
{% if latest_post %}
    <div class="jumbotron p-4 p-md-5 text-white rounded bg-dark">
        <div class="col-md-6 px-0">
        <h1 class="display-4 font-italic">
            {{ latest_post.title }}
        </h1>
        <p class="lead my-3">
            {{ latest_post.body|truncatewords:30 }}
        </p>
        <p class="lead mb-0">
            <a href="{% url 'blog:post' post.pk %}" class="text-white font-weight-bold">Continue reading...</a>
        </p>
        </div>
    </div>
{% endif %}

{% for post in posts %}  
<div class="row mb-2">
    <div class="col-md-6">
      <div
        class="row no-gutters border rounded overflow-hidden flex-md-row mb-4 shadow-sm h-md-250 position-relative"
      >
        <div class="col p-4 d-flex flex-column position-static">
          <h3 class="mb-0">{{ post.title }}</h3>
          <div class="mb-1 text-muted">{{ post.date_posted }}</div>
          <p class="mb-auto">
            {{ post.body|truncatewords:30 }}
          </p>
          <a href="{% url 'blog:post' post.pk %}" class="stretched-link">Continue reading</a>
{% endfor %}

{% endblock %}

但是,我收到此错误:

django.template.exceptions.TemplateSyntaxError: Unclosed tag on line 2: 'block'. Looking for one of: endblock. in django

我已经确定:

  1. 所有区块都关闭
  2. 百分号和块名称之间没有空格
  3. 我没有错过任何百分号

请帮我

标签: pythondjangojinja2

解决方案


在 for 循环中,您<div>在关闭它们之后有四个打开的元素,看起来没问题。

{% for post in posts %}  
<div class="row mb-2">
    <div class="col-md-6">
      <div class="row no-gutters border rounded overflow-hidden flex-md-row mb-4 shadow-sm h-md-250 position-relative">
        <div class="col p-4 d-flex flex-column position-static">
          <h3 class="mb-0">{{ post.title }}</h3>
          <div class="mb-1 text-muted">{{ post.date_posted }}</div>
          <p class="mb-auto">
            {{ post.body|truncatewords:30 }}
          </p>
            <a href="{% url 'blog:post' post.pk %}" class="stretched-link">Continue reading</a>
        </div></div></div></div>
{% endfor %}
        

推荐阅读