Invalid block tag on line 117: 'endblock', expected 'empty' or 'endfor'. Did you forget to register or load this tag?

huangapple go评论50阅读模式
英文:

Invalid block tag on line 117: 'endblock', expected 'empty' or 'endfor'. Did you forget to register or load this tag?

问题

It seems like you're experiencing an issue with your Django template code. The error message you're getting, "Invalid block tag on line 117: 'endblock', expected 'empty' or 'endfor'. Did you forget to register or load this tag?" indicates that there might be an issue with the use of the {% endblock %} tag.

To troubleshoot this issue, you can follow these steps:

  1. Check for Missing Load Statement:
    Ensure that you have loaded the static and other necessary template libraries at the top of your template file. You should have something like this at the beginning of your template:

    {% load static %}
    

    Make sure you have the appropriate {% load ... %} statement for any other template tags you might be using as well.

  2. Verify Block Hierarchy:
    Check if all your {% block ... %} tags have corresponding {% endblock %} tags. In your provided code, you seem to have a {% block body %} tag, and it appears that you have an {% endblock %} tag at the end of your template. Ensure that there are no unmatched block tags, and they are properly nested.

  3. Check for Syntax Errors:
    Sometimes, syntax errors or typos can cause issues like this. Double-check that there are no syntax errors in your template, such as mismatched {% endfor %} or {% endif %} tags. The error might be occurring earlier in the template, affecting the block definitions later on.

  4. Look for Included Templates:
    If you are extending another template (as indicated by {% extends 'shop/basic.html' %}), make sure that the base template (basic.html) also has the necessary block definitions and {% endblock %} tags. If you have extended a template and overridden a block, ensure that you have defined that block in the base template.

  5. Check for Template Caching:
    Sometimes, template caching can cause issues where changes to templates are not immediately reflected. You can try clearing your Django template cache to ensure that you are working with the most up-to-date templates.

  6. Review Your Django Version:
    Occasionally, issues like this may be related to specific versions of Django. Make sure you are using a compatible version of Django for your code.

  7. Debugging Output:
    To debug further, you can add some debugging output in your template. For example, you can use {% debug %} to display the template context, or print values using {{ variable }} to check if the variables are being set correctly.

Once you've gone through these troubleshooting steps, you should be able to identify the root cause of the issue and resolve it. It's important to carefully review your template code and ensure that all block tags are properly defined and matched with their respective {% endblock %} tags.

英文:
{% extends 'shop/basic.html' %}
{% block css %}
        .col-md-3 {
            display: inline-block;
            margin-left: -4px;
        }

        .col-md-3 {
            width: 100%;
            height: auto;
        }



        body .no-padding {
            padding-left: 0;
            padding-right: 0;
        }

        .carousel-control-prev-icon {
            background: black no-repeat center center;
            background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' viewBox='0 0 8 8'%3e%3cpath d='M5.25 0l-4 4 4 4 1.5-1.5-2.5-2.5 2.5-2.5-1.5-1.5z'/%3e%3c/svg%3e");
        }

        .carousel-control-next-icon {
            background: black no-repeat center center;
            background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' viewBox='0 0 8 8'%3e%3cpath d='M2.75 0l-1.5 1.5 2.5 2.5-2.5 2.5 1.5 1.5 4-4-4-4z'/%3e%3c/svg%3e");

        }
        body .carousel-indicators{
        bottom:0;
        }

        .carousel-indicators .active{
        background-color: blue;
        }


{% endblock css %}
  {% block body %}
  {% load static %}



<!--        carousel indicators starts from here-->

<div class="container" xmlns:data="http://www.w3.org/1999/xhtml">
    <div id="demo" class="carousel slide my-3" data-ride="carousel">'
        <ul class="carousel-indicators">
            <li data-target="#demo" data-slide-to="0" class="active"></li>
    {% for i in range %}
       <li data-target="#demo" data-slide-to="{{i}}" ></li>
    {% endfor %}
</ul>

<!--        slideshow starts here-->

<div class="carousel-inner">
  <div class="carousel-item active">

      <div class="col-xs-3 col-sm-3 col-md-3">
        <div class="card" style="width: 18rem;">
          <img src='/media/{{product.0.image}}' class="card-img-top" alt="...">
          <div class="card-body">
            <h5 class="card-title">Card title</h5>
            <p class="card-text">Some quick example text to build on the card title and make up the bulk of the
              card's content.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
      </div>

      {% for i in product|slice:"1:" %}
      <div class="col-xs-3 col-sm-3 col-md-3">
        <div class="card" style="width: 18rem;">
          <img src='/media/{{i.image}}' class="card-img-top" alt="...">
          <div class="card-body">
            <h5 class="card-title">Card title</h5>
            <p class="card-text">Some quick example text to build on the card title and make up the bulk of the
              card's content.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
      </div>

            {% for i in product|slice:"1:" %}
            <div class="col-xs-3 col-sm-3 col-md-3">
              <div class="card" style="width: 18rem;">
                <img src='{% static "shop/test.jpg" %}' class="card-img-top" alt="...">
                <div class="card-body">
                  <h5 class="card-title">Card title</h5>
                  <p class="card-text">Some quick example text to build on the card title and make up the bulk of the
                    card's content.</p>
                  <a href="#" class="btn btn-primary">Go somewhere</a>
                </div>
              </div>
            </div>

            {% if forloop.counter|divisibleby:3 and forloop.counter > 0 and not forloop.last %}
             </div><div class="carousel-item">
            {% endif%}

            {% endfor %}
      </div>

      </div>
        </div>

<a class="carousel-control-prev " href="#demo" role="button" data-slide="prev">
   <span class="carousel-control-prev-icon" aria-hidden="true" ></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#demo" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
  {% endblock %}
</div>

This was my code in index.html and I am getting this error:
Invalid block tag on line 117: 'endblock', expected 'empty' or 'endfor'. Did you forget to register or load this tag?

How to troubleshoot now?

THE ERROR I'M GETTING:
enter image description here

How to troubleshoot?
Debug the code for me please.
P.S: The browser is saying that endblock is missing but I added the endblock there.

答案1

得分: 0

你在第75行和88行有两个重复的for,并且在第106行有一个endfor。你需要处理这种情况,要么移除一个for,要么在该段落的末尾添加另一个endfor

英文:

You have two duplicate for in line 75 and 88 and one endfor in line 106. you have to handle this situation, remove one of the for or add another endfor at the end of that section.

答案2

得分: 0

以下是翻译好的部分:

错误是由于您的代码中重复的“for循环块”引起的。另外,在扩展基本模板后,您应该在脚本的顶部加载静态资源。

这段代码应该可以工作:

{% extends 'shop/basic.html' %}
{% load static %}
{% block css %}
    .col-md-3 {
        display: inline-block;
        margin-left: -4px;
    }

    .col-md-3 {
        width: 100%;
        height: auto;
    }

    body .no-padding {
        padding-left: 0;
        padding-right: 0;
    }

    .carousel-control-prev-icon {
        background: black no-repeat center center;
        background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' viewBox='0 0 8 8'%3e%3cpath d='M5.25 0l-4 4 4 4 1.5-1.5-2.5-2.5 2.5-2.5-1.5-1.5z'/%3e%3c/svg%3e");
    }

    .carousel-control-next-icon {
        background: black no-repeat center center;
        background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' viewBox='0 0 8 8'%3e%3cpath d='M2.75 0l-1.5 1.5 2.5 2.5-2.5 2.5 1.5 1.5 4-4-4-4z'/%3e%3c/svg%3e");
    }

    body .carousel-indicators {
        bottom: 0;
    }

    .carousel-indicators .active {
        background-color: blue;
    }
{% endblock css %}

{% block body %}
    <!-- 轮播指示器从这里开始 -->
    <div class="container" xmlns:data="http://www.w3.org/1999/xhtml">
        <div id="demo" class="carousel slide my-3" data-ride="carousel">
            <ul class="carousel-indicators">
                <li data-target="#demo" data-slide-to="0" class="active"></li>
                {% for i in range %}
                    <li data-target="#demo" data-slide-to="{{i}}"></li>
                {% endfor %}
            </ul>

            <!-- 幻灯片从这里开始 -->
            <div class="carousel-inner">
                <div class="carousel-item active">
                    <div class="col-xs-3 col-sm-3 col-md-3">
                        <div class="card" style="width: 18rem;">
                            <img src='/media/{{product.0.image}}' class="card-img-top" alt="...">
                            <div class="card-body">
                                <h5 class="card-title">Card title</h5>
                                <p class="card-text">一些快速示例文本,用于构建卡片标题和卡片内容的大部分内容。</p>
                                <a href="#" class="btn btn-primary">去某处</a>
                            </div>
                        </div>
                    </div>

                    {% for i in product %}
                        <div class="col-xs-3 col-sm-3 col-md-3">
                            <div class="card" style="width: 18rem;">
                                <img src='{% static "shop/test.jpg" %}' class="card-img-top" alt="...">
                                <div class="card-body">
                                    <h5 class="card-title">Card title</h5>
                                    <p class="card-text">一些快速示例文本,用于构建卡片标题和卡片内容的大部分内容。</p>
                                    <a href="#" class="btn btn-primary">去某处</a>
                                </div>
                            </div>
                        </div>

                        {% if forloop.counter|divisibleby:3 and forloop.counter > 0 and not forloop.last %}
                            <div class="carousel-item"></div>
                        {% endif %}
                    {% endfor %}
                </div>
            </div>
        </div>

        <a class="carousel-control-prev" href="#demo" role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="sr-only">上一个</span>
        </a>
        <a class="carousel-control-next" href="#demo" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only">下一个</span>
        </a>
    </div>
{% endblock %}

请注意,代码中的注释部分已被翻译为中文。

英文:

The error was caused by the duplicated for loop block in your code. Also you should load static at to top of the script, after extending the base template

This code should work:

{% extends &#39;shop/basic.html&#39; %}
{% load static %}
{% block css %}
.col-md-3 {
display: inline-block;
margin-left: -4px;
}
.col-md-3 {
width: 100%;
height: auto;
}
body .no-padding {
padding-left: 0;
padding-right: 0;
}
.carousel-control-prev-icon {
background: black no-repeat center center;
background-image: url(&quot;data:image/svg+xml,%3csvg xmlns=&#39;http://www.w3.org/2000/svg&#39; fill=&#39;%23fff&#39; viewBox=&#39;0 0 8 8&#39;%3e%3cpath d=&#39;M5.25 0l-4 4 4 4 1.5-1.5-2.5-2.5 2.5-2.5-1.5-1.5z&#39;/%3e%3c/svg%3e&quot;);
}
.carousel-control-next-icon {
background: black no-repeat center center;
background-image: url(&quot;data:image/svg+xml,%3csvg xmlns=&#39;http://www.w3.org/2000/svg&#39; fill=&#39;%23fff&#39; viewBox=&#39;0 0 8 8&#39;%3e%3cpath d=&#39;M2.75 0l-1.5 1.5 2.5 2.5-2.5 2.5 1.5 1.5 4-4-4-4z&#39;/%3e%3c/svg%3e&quot;);
}
body .carousel-indicators{
bottom:0;
}
.carousel-indicators .active{
background-color: blue;
}
{% endblock css %}
{% block body %}
&lt;!--   carousel indicators starts from here--&gt;
&lt;div class=&quot;container&quot; xmlns:data=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;div id=&quot;demo&quot; class=&quot;carousel slide my-3&quot; data-ride=&quot;carousel&quot;&gt;&#39;
&lt;ul class=&quot;carousel-indicators&quot;&gt;
&lt;li data-target=&quot;#demo&quot; data-slide-to=&quot;0&quot; class=&quot;active&quot;&gt;&lt;/li&gt;
{% for i in range %}
&lt;li data-target=&quot;#demo&quot; data-slide-to=&quot;{{i}}&quot; &gt;&lt;/li&gt;
{% endfor %}
&lt;/ul&gt;
&lt;!--        slideshow starts here--&gt;
&lt;div class=&quot;carousel-inner&quot;&gt;
&lt;div class=&quot;carousel-item active&quot;&gt;
&lt;div class=&quot;col-xs-3 col-sm-3 col-md-3&quot;&gt;
&lt;div class=&quot;card&quot; style=&quot;width: 18rem;&quot;&gt;
&lt;img src=&#39;/media/{{product.0.image}}&#39; class=&quot;card-img-top&quot; alt=&quot;...&quot;&gt;
&lt;div class=&quot;card-body&quot;&gt;
&lt;h5 class=&quot;card-title&quot;&gt;Card title&lt;/h5&gt;
&lt;p class=&quot;card-text&quot;&gt;Some quick example text to build on the card title and make up the bulk of the
card&#39;s content.&lt;/p&gt;
&lt;a href=&quot;#&quot; class=&quot;btn btn-primary&quot;&gt;Go somewhere&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
{% for i in product %}
&lt;div class=&quot;col-xs-3 col-sm-3 col-md-3&quot;&gt;
&lt;div class=&quot;card&quot; style=&quot;width: 18rem;&quot;&gt;
&lt;img src=&#39;{% static &quot;shop/test.jpg&quot; %}&#39; class=&quot;card-img-top&quot; alt=&quot;...&quot;&gt;
&lt;div class=&quot;card-body&quot;&gt;
&lt;h5 class=&quot;card-title&quot;&gt;Card title&lt;/h5&gt;
&lt;p class=&quot;card-text&quot;&gt;Some quick example text to build on the card title and make up the bulk of the
card&#39;s content.&lt;/p&gt;
&lt;a href=&quot;#&quot; class=&quot;btn btn-primary&quot;&gt;Go somewhere&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
{% if forloop.counter|divisibleby:3 and forloop.counter &gt; 0 and not forloop.last %}
&lt;div class=&quot;carousel-item&quot;&gt;&lt;/div&gt;
{% endif%}
{% endfor %}
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;a class=&quot;carousel-control-prev &quot; href=&quot;#demo&quot; role=&quot;button&quot; data-slide=&quot;prev&quot;&gt;
&lt;span class=&quot;carousel-control-prev-icon&quot; aria-hidden=&quot;true&quot; &gt;&lt;/span&gt;
&lt;span class=&quot;sr-only&quot;&gt;Previous&lt;/span&gt;
&lt;/a&gt;
&lt;a class=&quot;carousel-control-next&quot; href=&quot;#demo&quot; role=&quot;button&quot; data-slide=&quot;next&quot;&gt;
&lt;span class=&quot;carousel-control-next-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;sr-only&quot;&gt;Next&lt;/span&gt;
&lt;/a&gt;
{% endblock %}
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年7月10日 13:51:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定