【问题标题】:Django project file - Cannot spot the mistake: "Invalid block tag: 'static'Django 项目文件 - 无法发现错误:“无效的块标签:'静态'
【发布时间】:2019-04-24 21:07:53
【问题描述】:

我有以下错误

Invalid block tag on line 4: 'static'. Did you forget to register or load this tag?

它是由这段代码生成的(见下文),但我无法(即使在查看了各种类似的问题和答案之后)发现错误。

<html>

<head>
<link rel="stylesheet" href="{% static 'wgb/styles.css' %}"/>

</head>

<body>
<div class="login">
    <h1>Login</h1>
    <form method="post">
        <input type="text" name="u" placeholder="Username" required="required" />
        <input type="password" name="p" placeholder="Password" required="required" />
        <button type="submit" class="btn btn-primary btn-block btn-large">Let me in.</button>
    </form>
</div>


</body>
</html>

错误明显在这一行:

<link rel="stylesheet" href="{% static 'wgb/styles.css' %}"/>

但我已经检查了空格、顺序和拼写/语法,但这些似乎都不是问题。

更具体地说,错误说明: 模板渲染时出错,它指向:&lt;link rel="stylesheet" href="{% static 'worldguestbook/styles.css' %}"/&gt;

【问题讨论】:

    标签: django templates static rendering


    【解决方案1】:

    您需要先加载静态模板标签,然后才能使用它。您通常在模板文件的顶部执行此操作:

    {% load static %}
    
    <html>
    <head>
    ....
    

    来源Django docs: Managing static files (e.g. images, JavaScript, CSS)

    【讨论】:

      猜你喜欢
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 2018-11-07
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 2011-07-18
      相关资源
      最近更新 更多