首页 > 解决方案 > /classes/aerobics/ 处的 TemplateSyntaxError 第 6 行的块标记无效:'set',预期为 'endblock'。您是否忘记注册或加载此标签?

问题描述

我正在尝试使用 for 循环呈现页面以在页面上的 3 列上显示视频对象,直到没有更多视频可显示为什么我会收到此错误?我要注册/加载什么标签才能使它起作用?第 6 行的这一行 ({% set count = 0 %}) 有什么问题?我相信我在正确的位置(最后一行)有 {% endblock %}。应该还有一个吗?

视图.py

@login_required
def classes_aerobics(request):
    obj = Video.objects.filter(category='Aerobics')
    return render(request, 'clubex/aerobics.html', {'obj': obj })

有氧运动.html

{% extends "ClubEx/base.html" %}
{% load embed_video_tags %}

{% block content %}

{% set count = 0 %}
{% for i in obj.all %}

    {% if count == 0 %}
        <div class="row">
    {% endif %}
    <div class="col-md-4">
        <div class="thumbnail">

            <div class="caption">
                {% video i.video 'tiny' %}
                <p>{{ i.content  }}</p>
                <div class="rw-ui-container"></div>
            </div>
        </div>
    </div>
    {% set count = count + 1 %}
    {% if count == 3 %}
        </div>
        {% set count = 0; %}
    {% endif %}
{% endfor %}
 {% endset %}   
{% endblock%}

回溯错误:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/classes/aerobics/

Django Version: 2.2.5
Python Version: 3.8.2
Installed Applications:
['ClubEx.apps.ClubexConfig',
 'users.apps.UsersConfig',
 'crispy_forms',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'embed_video']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template D:\NewAgileProjectRepo\NewAgileProjectRepo\ClubEx\templates\clubex\aerobics.html, error at line 6
   Invalid block tag on line 6: 'set', expected 'endblock'. Did you forget to register or load this tag?
   1 : {% extends "ClubEx/base.html" %}
   2 : {% load embed_video_tags %}
   3 : 
   4 : {% block content %}
   5 : 
   6 :  {% set count = 0 %} 
   7 : {% for i in obj.all %}
   8 : 
   9 :     {% if count == 0 %}
   10 :         <div class="row">
   11 :     {% endif %}
   12 :     <div class="col-md-4">
   13 :         <div class="thumbnail">
   14 : 
   15 :             <div class="caption">
   16 :                 {% video i.video 'tiny' %}


Traceback:

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py" in parse
  470.                     compile_func = self.tags[command]

During handling of the above exception ('set'), another exception occurred:

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  21.                 return view_func(request, *args, **kwargs)

File "D:\NewAgileProjectRepo\NewAgileProjectRepo\ClubEx\views.py" in classes_aerobics
  30.     return render(request, 'clubex/aerobics.html', {'obj': obj })

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\shortcuts.py" in render
  36.     content = loader.render_to_string(template_name, context, request, using=using)

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\loader.py" in render_to_string
  61.         template = get_template(template_name, using=using)

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\loader.py" in get_template
  15.             return engine.get_template(template_name)

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\backends\django.py" in get_template
  34.             return Template(self.engine.get_template(template_name), self)

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\engine.py" in get_template
  143.         template, origin = self.find_template(template_name)

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\engine.py" in find_template
  125.                 template = loader.get_template(name, skip=skip)

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\loaders\base.py" in get_template
  29.                 return Template(

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py" in __init__
  156.         self.nodelist = self.compile_nodelist()

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py" in compile_nodelist
  194.             return parser.parse()

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py" in parse
  478.                     raise self.error(token, e)

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py" in parse
  476.                     compiled_result = compile_func(self, token)

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\loader_tags.py" in do_extends
  266.     nodelist = parser.parse()

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py" in parse
  478.                     raise self.error(token, e)

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py" in parse
  476.                     compiled_result = compile_func(self, token)

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\loader_tags.py" in do_block
  209.     nodelist = parser.parse(('endblock',))

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py" in parse
  472.                     self.invalid_block_tag(token, command, parse_until)

File "C:\Users\Charlie\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py" in invalid_block_tag
  522.             raise self.error(

Exception Type: TemplateSyntaxError at /classes/aerobics/
Exception Value: Invalid block tag on line 6: 'set', expected 'endblock'. Did you forget to register or load this tag?

标签: python-3.xdjangofor-looptemplateshtml-rendering

解决方案


推荐阅读