Django-模板

这篇讲什么

  • Django 模板语言(DTL)不是 Python,它只有三样东西:变量负责填值、标签负责控制流程、过滤器用竖线接在变量后面做加工。
  • 这篇讲点语法的查找顺序、过滤器「静默变空」的行为、常用过滤器对照表、模板继承与 include、static / url / csrf_token 三个必用标签、自定义过滤器与标签,以及模板到底是怎么被找到的。基线 Django 5.2.19。

模板是什么

  • 模板就是带占位符的 HTML:视图把数据(上下文)传进来,模板引擎填充后返回完整页面。Django 默认的模板语言叫 DTL(Django Template Language),它不是 Python,只有变量、过滤器、标签三样东西。

  • 三种语法元素:

    1
    2
    3
    {{ 变量 }}                {# 取值,可以接过滤器 #}
    {% 标签 %} {# 逻辑:if / for / block / include ... #}
    {# 注释,不会输出 #}
  • 本篇所有输出都是在 Django 5.2.19 上渲染出来的真实结果。

变量的点语法

  • 一个 . 会依次尝试:字典的键 → 属性/方法 → 列表下标。实测(同一个 key 既是字典键又有同名属性时,字典键赢):

    上下文 模板 实测输出
    {"b": "dict-key"} {{ a.b }} dict-key
    字典有 keys 键 {{ a.keys }} dict-key-wins(不是方法)
    对象有 __getitem__ {{ a.b }} getitem-b(下标兜底)
    ["x", "y"] {{ a.1 }} y
    无参方法 meth() {{ stu.meth }} meth-result(自动调用)
  • 三条硬限制,新手最容易在这里撞:

    1
    2
    3
    {{ a['b'] }}          TemplateSyntaxError: Could not parse the remainder: '['b']' from 'a['b']'
    {{ a.-1 }} Could not parse the remainder: '-1' from 'a.-1'
    {{ stu.need_arg 5 }} Could not parse the remainder: ' 5' from 'stu.need_arg 5'
    • DTL 不支持方括号下标、不支持负索引、调用方法时不能传参数。要带参的数据,一律在视图里算好再塞进上下文。
    • 变量名里也不能有连字符:{{ a.b-c }} → Could not parse the remainder: '-c' from 'a.b-c'。
  • 变量不存在时默认静默输出空串(不报错):

    1
    2
    [{{ missing }}]      ->  []
    {{ value|length }} -> 0 (未定义变量走过滤器,得到 0)

用 string_if_invalid 把「取不到值」暴露出来

  • 在 TEMPLATES 的 OPTIONS 里加:

    settings.py
    1
    2
    3
    4
    5
    6
    TEMPLATES = [{
    "BACKEND": "django.template.backends.django.DjangoTemplates",
    "DIRS": [BASE_DIR / "templates"],
    "APP_DIRS": True,
    "OPTIONS": {"string_if_invalid": "[MISSING:%s]"},
    }]

    实测:

    1
    2
    3
    4
    5
    {{ nope }}            ->  [MISSING:nope]
    {{ a.nope.deep }} -> [MISSING:a.nope.deep]
    {{ nope|length }} -> [MISSING:nope] (过滤器被整个跳过)
    {{ nope|default:'d' }}-> [MISSING:nope] (default 也失效!)
    {% if nope %} -> 走 False 分支
    • 结论:这个选项只适合调试时用。一旦设上,default、length 这类「指望空值行为」的过滤器全部失真。

过滤器:语法与「静默变空」

  • 写法是 {{ 变量|过滤器:参数 }},可以链式:{{ value|lower|truncatechars:20 }}。

  • 类型不对时,大多数过滤器不报错,而是给你空串——这是 DTL 最阴的地方。实测:

    用法 实测输出
    {{ "abc"|add:2 }} 空串(字符串加整数失败,静默)
    {{ "text"|add:'0' }} text0(两边都是字符串时是拼接)
    {{ s|date:"Y-m-d" }}(s 是字符串) 空串
    {{ "abc"|floatformat:2 }} 空串
    {{ 5|slice:":2" }} 空串(int 不可切片)
    {{ 42|random }} 500:TypeError: object of type 'int' has no len()
    {{ s|timesince }}(s 是字符串) 500:AttributeError: 'str' object has no attribute 'year'
    • 也就是说:「静默变空」和「直接 500」两种都有,别猜,测。
  • 竖线在模板里就是分隔符,{{ value| }}(结尾多一个竖线)会报 Could not parse the remainder: '|' from 'value|'。

常用过滤器实测对照

字符串

过滤器 输入 实测输出 备注
lower / upper / title / capfirst "jOEL is a Slug" joel is a slug / JOEL IS A SLUG / Joel Is A Slug / Joel is a slug title 每个词首字母大写
slugify "Joel is a slug" joel-is-a-slug 中文会被全部丢掉:Python 3.9 中文测试 → python-39
truncatechars:7 "Joel is a slug" Joel i… 省略号算进长度,是单个字符 U+2026
truncatewords:2 "Joel is a slug" Joel is … 注意 is 后面还有一个空格再接省略号
center:"15" "Django" ' Django '(左 5 右 4) 老笔记写的「左 3 右 3」是错的,宽度不够时 Python 的 center 偏向右边
ljust:"10" / rjust:"10" "Django" 'Django ' / ' Django' 同上,老笔记的空格数不对
cut:" " "String with spaces" Stringwithspaces 删的是所有匹配子串,不只是空格
make_list "Joel" ['J', 'o', 'e', 'l'] 页面源码里引号是 '(被自动转义)
urlencode "a b&c" a%20b%26c 用于拼查询串
striptags "<b>hi</b>" hi 粗暴删标签,不是清洗

数字与格式

过滤器 输入 实测输出
add:2 4 6
floatformat:3 34.23234 34.232
floatformat 34.23234 34.2
floatformat:"-2" 34.23234 34.23
filesizeformat 12345678 11.8\xa0MB
stringformat:"E" 10 1.000000E+01
divisibleby:3 9 / 10 True / False
get_digit:"8" 电话号码 取第 8 位数字;参数越界返回 1,参数 0 返回原值
  • filesizeformat、timesince、timeuntil 输出的「空格」是不间断空格 U+00A0(实测 REPR '11.7\xa0MB')。复制粘贴到别处或做字符串比对时会不一样,这是老笔记里看不出来的坑。

日期时间

过滤器 实测输出
date(默认格式) June 1, 2006
date:"Y-m-d H:i:s" 2026-11-04 22:13:18
time:"H:i" 01:23
timesince(4 天 6 小时前) 4\xa0days, 6\xa0hours
timesince(8 小时前) 8\xa0hours
timesince(1 周前) 1\xa0week
timeuntil(28 天后) 3\xa0weeks, 6\xa0days
  • 老笔记把 timeuntil 的例子写成「4 weeks」,实测 28 天给的是 3 weeks, 6 days(它按 7 天进位后再算余数)。
  • USE_TZ=True(新项目默认)时,给 date/timesince 传 naive datetime 会出问题,务必用 aware 时间(django.utils.timezone.now())。

列表与字典

过滤器 输入 实测输出
length 4 元素列表 / 3 键字典 / 字符串 4 / 3(数字典是键数) / 4
length 未定义变量 / 整数 / None 0 / 0 / 0
first / last [1,2,3] 1 / 3
join:", " ["a","b"] a, b
slice:"2:5" "abcdefg" cde
dictsort:"age" 字典列表 按 age 键排序
unordered_list ["States",["Kansas",["Lawrence","Topeka"]],"Illinois"] 见下面
  • unordered_list 的真实输出用的是制表符缩进(老笔记写成了空格):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <li>States
    <ul>
    <li>Kansas
    <ul>
    <li>Lawrence</li>
    <li>Topeka</li>
    </ul>
    </li>
    <li>Illinois</li>
    </ul>
    </li>

默认值与判空

过滤器 输入 实测输出
default:"x" 空串 / 未定义 x / x
default_if_none:"x" None x
default_if_none:"x" 空串 空串(只有 None 才兜)
default_if_none:"x" 变量不存在 空串(不是 x!变量缺失时整个表达式被跳过)

HTML 与安全类

过滤器 实测输出
linebreaks 包一层 <p>,行间空行变 <br>
linebreaksbr 只把 \n 换成 <br>;老笔记示例里写的是 /n(斜杠),那样原样输出 Joel/nis a slug
escape 输入 <a>&"'b</a> → &lt;a&gt;&amp;&quot;&#x27;b&lt;/a&gt;
escapejs testing\r\njavascript 'string" <b>escaping</b> → testing\u000D\u000Ajavascript \u0027string\u0022 \u003Cb\u003E escaping \u003C/b\u003E
force_escape 两次 &amp;lt;b&amp;gt;(可以多轮,escape 只转一轮)
safe <h1>hi</h1> 原样输出;再接 |escape 也不再转义
json_script:"hello-data" <script id="hello-data" type="application/json">{"hello": "world"}</script>
truncatechars_html:7 输入必须是 SafeString 或关掉 autoescape,否则整段被转义成 &lt;p&gt;Joel i…&lt;/p&gt;
iriencode ?test=1&amp;me=2(& 会被自动转义,老笔记的输出少了一层)

其余过滤器(实测)

过滤器 输入 实测输出
safeseq + join 列表 ["<b>a</b>", "b"] <b>a</b>, b(逐元素处理后再拼接;直接对列表用 escape 会把整个列表当字符串转义)
truncatewords_html:2 "<p>Joel is a slug</p>" &lt;p&gt;Joel is …&lt;/p&gt;;前面加 safe 才是 <p>Joel is …</p>
urlize "check http://django.us and joel@isnotaslug.com" check <a href="http://django.us" rel="nofollow">…</a> and <a href="mailto:joel@isnotaslug.com">…</a>
urlizetrunc:15 "http://django.us/downloads/" <a href="http://django.us/downloads/" rel="nofollow">http://django.…</a>
wordwrap:10 "Joel is a slug on the web" Joel is a\nslug on\nthe web
wordcount "Joel is a slug" 4
linenumbers "one\ntwo\nthree" 1. one\n2. two\n3. three
pluralize {{ v|add:1|pluralize }} / {{ v|add:2|pluralize }} s / s(1 不加、其余加)
pluralize:"s,es" 值为 0 es(单数用第一项、复数用第二项)
phone2numeric "800-COLLECT" 800-2655328
yesno 配合 "yes,maybe,no" 三态映射,实测与官方示例一致
get_digit 电话号码 + "8" 取第 8 位;参数越界返回 1,参数 0 返回原值
  • escape 那五条替换在老笔记里被 Markdown 吞成了裸符号,这里给准确对应:< → &lt;、 > → &gt;、 & → &amp;、 " → &quot;、 ' → &#x27;。

标签

{% if %} 家族

  • 实测支持的运算符:>、 >=、 <、 <=、 ==、 !=、 in、 not in、 is None、 is not None、 is True、 is 5、 not、 and、 or,以及多个 {% elif %}。

    1
    2
    3
    {% if num > 15 %}大{% elif num == 15 %}中{% else %}小{% endif %}
    {% if name in allowed %}…{% endif %}
    {% if user is not None %}…{% endif %}
  • 老式文字运算符已经删除:

    1
    {% if a gt 1 %}  ->  TemplateSyntaxError: Unused 'gt' at end of if expression.

{% for %}

用法 实测输出(数据 [1,2,3])
{{ forloop.counter }} 1|2|3|
{{ forloop.counter0 }} 0|1|2|
{{ forloop.first }} TrueFalseFalse
{{ forloop.revcounter }} / revcounter0 322110
{% for x in lst reversed %} 321
{% for x in lst %}…{% empty %}空{% endfor %} 空列表和变量不存在都走 empty 分支
{% for k, v in pairs %} 元组解包可用,输出 1=2;3=4;
嵌套循环里 {{ forloop.parentloop.counter }} 拿到外层序号

其他常用标签(实测)

标签 实测结果
{% cycle 'a' 'b' %} ababa;带分组名 {% cycle 'g1' 'a' 'b' %} 会把 g1 也打印出来(坑)
{% widthratio num 1 2 %} 6;{% widthratio num 2 1 %} → 2(四舍五入);分母为 0 → 0;非数字 → 空
{% now 'Y-m-d H:i:s' %} 2026-11-04 22:13:18;{% now 'c' %} → 带 +08:00;不带参数 → 'now' statement takes one argument
{% with total=num1|add:num2 %}…{% endwith %} 只在块内生效
{% spaceless %} 只删标签之间的空白:<p>\n <a>Foo</a>\n</p> → <p><a href='foo/'>Foo</a></p>;但 <b>x</b> <i>y</i>(文本之间的空格)不动
{% verbatim %} 里面的 {{ }} 原样输出,用来写「讲模板的模板」
{% comment %}…{% endcomment %} 全删;{% comment %} 后面可以直接跟说明文字
{% filter upper %} 块内可放标签:{% filter upper %}{% now 'Y' %} abc{% endfilter %} → 2026 ABC
{% firstof a b '默认' %} 输出第一个非空变量;字面量参数不转义(写 '<b>lit</b>' 会直接出 HTML,注意)
{% lorem %} 默认 3 段拉丁文
{% autoescape off %}…{% endautoescape %} 块内不转义:<a>&"'b</a> 原样输出
{% ifchanged %}{{ n }}{% endifchanged %} 数据 [1,1,2] → 12;只跟上一次迭代比,值变回去也会输出(['a','b','a'] → aba)
{% regroup people by age as grouped %} {{ g.grouper }}:{{ g.list|length }} → 1:2;2:1;
{% localize off %} 需要先 {% load l10n %},否则 Invalid block tag on line 1: 'localize';生效后 1234567.89

已经删掉的标签和过滤器

  • 老笔记里这些写法在 5.2 上直接报错,实测原文:

    老写法 5.2 实测报错 替代
    {% ifequal a b %} TemplateSyntaxError: Invalid block tag on line 1: 'ifequal'. Did you forget to register or load this tag? {% if a == b %}
    {% ifnotequal a b %} 同上句式 {% if a != b %}
    {% ssi /path %} Invalid block tag on line 1: 'ssi'. … 没有替代(因安全原因废弃)
    {{ value\|length_is:"4" }} TemplateSyntaxError: Invalid filter: 'length_is' {% if value\|length == 4 %}
    {% include "p.html" using "ctxkey" %} TemplateSyntaxError: Unknown argument for 'include' tag: 'using'. with / only
    {% load static from staticfiles %} 'staticfiles' is not a registered tag library. Must be one of: admin_list admin_modify admin_urls cache filters i18n l10n log mytags static tz {% load static %}
    {% if a gt 1 %} Unused 'gt' at end of if expression. {% if a > 1 %}
  • 一条别改错的:{% trans %} / {% blocktrans %} 在 5.2 上没有弃用、也不发警告(实测用 warnings.catch_warnings(record=True) 捕获,结果 WARN| none)。新名 {% translate %} / {% blocktranslate %} 也已实测可用,两者都能写。

  • 已删除的设置项:USE_L10N(5.0 起没了),要局部关本地化改用 {% localize off %}。

模板继承

  • 基本结构:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <!DOCTYPE html>
    <html lang="zh">
    <head>
    <title>{% block title %}默认标题{% endblock %}</title>
    </head>
    <body>
    {% block content %}{% endblock %}
    </body>
    </html>
    1
    2
    3
    4
    5
    6
    7
    8
    {% extends "base.html" %}

    {% block title %}子标题{% endblock %}

    {% block content %}
    {{ block.super }} {# 父块内容,实测输出 PARENT-MAIN #}
    子页面正文
    {% endblock %}
  • 实测的六条规则(都是真实报错原文):

    规则 违反时的报错
    {% extends %} 必须是第一个标签(前面可以有纯文本) {% extends 'super_base.html' %} must be the first tag in the template.
    一个模板只能 extends 一次 'extends' cannot appear more than once in the same template
    不能把 {% extends %} 写进 {% block %} 里 Invalid block tag on line 1: 'endblock'. Did you forget to register or load this tag?
    {% endblock 名字 %} 必须和 {% block 名字 %} 对上 expected 'endblock' or 'endblock a'
    同一模板里 block 名不能重复 'block' tag with name 'a' appears more than once
    子模板里写了父模板没有的 block 静默丢弃,不报错
    • 老笔记说「extends 必须写在第一行」,准确说法是「第一个标签」:实测在 {% extends %} 之前放纯文本是合法的,那段文本还会被输出。
    • {{ block.super }} 在父模板自己里渲染时返回空(没有上一层)。

include

  • 四种写法实测:

    1
    2
    3
    4
    {% include "partial.html" %}                       {# 默认能拿到当前上下文 #}
    {% include "partial.html" only %} {# 只给显式传的 #}
    {% include "partial.html" with extra='from-with' %} {# 覆盖/追加一个变量 #}
    {% include tpl_name_var %} {# 模板名可以是变量 #}
    • 实测 only 时未传的变量在片段里就是 NONE:PARTIAL(name=NONE, extra=NONE);with 只影响指定的那一项,其余上下文照旧。
    • 老笔记里的 using 写法已删(见上表)。

static、 url、 csrf_token

  • {% static %}

    1
    2
    {% load static %}
    <link rel="stylesheet" href="{% static 'css/index.css' %}">
    • 实测渲染成 /static/css/index.css,runserver 请求它返回 HTTP/1.1 200 OK … Content-Type: text/css。
    • 忘记 {% load static %} 时报错:Invalid block tag on line 1: 'static'. Did you forget to register or load this tag?
  • {% url %}

    myapp/urls.py
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    from django.urls import path

    from myapp import views

    app_name = "sunck"

    urlpatterns = [
    path("t3test/<int:pk>/", views.t3test, name="t3test"),
    path("t3test_kw/<int:num>/", views.t3test, name="t3test_kw"),
    ]
    1
    2
    3
    4
    5
    {% url 'sunck:t3test' 5 %}            ->  /t3test/5/
    {% url 'sunck:t3test_kw' num=7 %} -> /t3test_kw/7/
    {% url 'sunck:t3test' 5 as the_url %} -> 存进变量,不直接输出
    {% url 'sunck:nope' %} -> NoReverseMatch: Reverse for 'nope' not found.
    'nope' is not a valid view function or pattern name.
    • 中文参数会被 URL 编码:/t8/%E4%B8%AD%E6%96%87/;查询串里的 & 在页面源码里是 &amp;(自动转义,浏览器解析后正确)。
    • 命名空间来自被包含 urls.py 里的 app_name,不要写在 include(..., namespace=...) 里(见《Django-视图》)。
  • {% csrf_token %}

    • 实测在有 RequestContext 时渲染成:

      1
      <input type="hidden" name="csrfmiddlewaretoken" value="axUTNg0C…">
    • 用 render_to_string 却没传 request 时,输出空字符串并留下警告(实测原文):

      1
      2
      UserWarning: A {% csrf_token %} was used in a template, but the context did not provide
      the value. This is usually caused by not using RequestContext.
    • 纠正一个常见误解:隐藏域的值不等于 Cookie 里的 csrftoken。实测同一次响应里 COOKIE=4z5X5MEZ…(32 位)、HIDDEN=axUTNg0C…(64 位),两者不相等 —— Cookie 存的是 secret,表单存的是掩码 token。

    • POST 校验的三种实测结果:正确 token → 200;token 错 → 403 Forbidden (CSRF token from POST incorrect.): /csrf/;没带 → 403 Forbidden (CSRF token missing.): /csrf/。

自定义过滤器与标签

  • 目录结构是硬要求:

    1
    2
    3
    4
    myapp/
    templatetags/
    __init__.py
    mytags.py
    • 应用必须在 INSTALLED_APPS 里,否则 {% load mytags %} 找不到;__init__.py 在 3.x 的命名空间包下不是必须(实测删掉后仍能加载),但建议保留以免踩坑。
    • 模块里语法错误/导入异常会直接抛真异常,不是变成模板错误 —— 报错时先看 traceback 里的文件名。
  • 一个自定义过滤器/标签库的完整样子:

    myapp/templatetags/mytags.py
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    from django import template
    from django.utils.safestring import mark_safe

    register = template.Library() # 老写法 from django.template import Library 也有效


    @register.filter
    def is_even(value): # 名字按 PEP8 用下划线
    return int(value) % 2 == 0


    @register.filter
    def red(value):
    return mark_safe("<span style='color:red'>%s</span>" % value)


    @register.filter(needs_autoescape=True)
    def echo_html(value, autoescape=True):
    if autoescape:
    from django.utils.html import escape
    return mark_safe("<i>%s</i>" % escape(value))
    return "<i>%s</i>" % value


    @register.simple_tag
    def current_year():
    import datetime
    return datetime.datetime.now().year
    1
    2
    3
    {% load mytags %}
    {{ 8|is_even }} -> True
    {% current_year %} -> 2026
  • is_safe=True 的真实语义(实测):它不是「输出免转义」,而是「当入参本身是 SafeString 时才免转义」。

    1
    2
    {{ v|red }}   v 是普通字符串  ->  &lt;span style='color:red'&gt;…&lt;/span&gt;
    {{ v|red }} v 经过 mark_safe -> <span style='color:red'>…</span>
  • 标签库重名会被覆盖并报错(实测):把自己的文件命名为 static.py 后——

    1
    2
    (templates.W003) 'static' is used for multiple template tag modules:
    'django.templatetags.static', 'myapp.templatetags.static'

    之后 {% load static %}{% static 'css/index.css' %} 变成 Invalid block tag on line 1: 'static',因为自定义库把内置库顶掉了。所以 templatetags 下的文件名别和内置库重名(static、 i18n、 l10n、 cache、 tz、 filters、 log、 admin_*)。

模板是怎么被找到的

  • 查找顺序实测(Engine.find_template() 抛出的候选列表):

    1
    2
    3
    4
    1. templates\no_such_template.html                       (DIRS)
    2. ...\django\contrib\admin\templates\...
    3. ...\django\contrib\auth\templates\...
    4. myapp\templates\... (APP_DIRS)
    • 即 DIRS 先,然后按 INSTALLED_APPS 顺序扫每个应用的 templates/;同名模板工程目录里的赢(实测 get_template('order_probe.html').origin.name 指向 templates\order_probe.html)。
  • TemplateDoesNotExist 的文本只有一句模板名(实测):

    1
    django.template.exceptions.TemplateDoesNotExist: does_not_exist_anywhere.html
    • 想看到「找过哪些路径」要用 Engine.find_template,loader.get_template() 那层的 exc.tried 是空列表。
  • cached.Loader 是默认,跟 DEBUG 无关(实测):

    1
    2
    loaders = [('django.template.loaders.cached.Loader',
    ['...filesystem.Loader', '...app_directories.Loader'])]
    • 实测同一个引擎里改文件后再渲染,cached after edit -> VERSION-1(陈旧),不缓存的那份 -> VERSION-2。
    • 开发时为什么改模板能立刻生效?因为 runserver 的模板自动重载会调 reset_loaders()。用 --noreload 起服务时改模板不会生效,这是很多人以为「缓存没刷新」的真实原因。
  • startproject 生成的默认 TEMPLATES(实测 5.2):"DIRS": [],context_processors 只有 request / auth / messages 三个(没有 debug),STATIC_URL = "static/"。

context_processors 注入了什么

  • 实测用 render_to_string(..., request=req) 渲染后:

    1
    path=[/t6/]  user=[ReqUser]  auth=[True]  msgs=[NONE]  csrf=[HAS-CSRF]

    不传 request 时全部为空:path=[] user=[] … csrf=[NO-CSRF]。

  • 所以「模板里 {{ user.username }} 时有时无」的根因通常是:视图没用 render()(或 RequestContext),或者 context_processors 里少了 django.template.context_processors.request。

  • debug 处理器只在 request.META.REMOTE_ADDR 属于 INTERNAL_IPS 时注入。实测默认 INTERNAL_IPS = [],所以 {{ DEBUG }} 是空、{% debug %} 也不会带 SQL 查询信息;本地要看得设 INTERNAL_IPS = ["127.0.0.1"]。

  • {% debug %} 本身可用,实测输出约 115 KB 且被转义 —— 别在生产页面里留它。

新手卡点清单

  • 都是实测过的:

    卡点 症状 正确做法
    用 {{ a['b'] }} Could not parse the remainder: '['b']' 只能用点语法 {{ a.b }}
    模板里给方法传参 Could not parse the remainder: ' 5' 视图里算好再传
    变量名/键名带连字符 Could not parse the remainder: '-c' 换下划线
    类型不匹配 多数静默变空串,少数直接 500(random、 timesince) 视图里保证类型;调试用 string_if_invalid
    抄了 ifequal / ssi / length_is / include ... using Invalid block tag / Invalid filter 用上表的替代写法
    {% extends %} 前写了别的标签 must be the first tag in the template. 纯文本可以,标签不行
    子模板写了父类没有的 block 静默不生效 先在父模板里开好块
    忘 {% load static %} / {% load mytags %} Invalid block tag on line 1: 'static' 用哪个库先 load 哪个
    templatetags 文件名撞内置库 templates.W003 + 之后标签全失效 换名字
    --noreload 起服务后改模板 页面还是旧的 cached.Loader 生效中,重启或去掉 --noreload
    以为 escape 会多轮转义 二次 |escape 结果不变 需要多轮用 force_escape

参考

本文结束 感谢您的阅读
正在加载今日诗词....