模板语法错误位于/accounts/profile/

2024-04-19 06:55:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有个错误:

TemplateSyntaxError at /accounts/profile/
<ExtendsNode: extends "registration/accounts/base.html"> must be the first tag in the template

我写了base.html

{% load staticfiles %}
<html lang="ja">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="{% load staticfiles 'bootflat/css/bootflat.min.css' %}">
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  </head>
  <body>
    <nav class="navbar navbar-default" role="navigation">
        <div class="navbar-header">
            <p class="navbar-text">HELLO</p>
            {% if user.is_authenticated %}
            <p class="navbar-text">{{  user.get_username }}</p>
            {% endif %}
        </div>
    </nav>

    <div class="container">
      {% block content %}
      {% endblock %}
    </div>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="{% static 'bootstrap/js/bootstrap.min.js' %}"></script>
  </body>
</html>

profile.html是:

{% load staticfiles %}
{% extends "registration/accounts/base.html" %}
{% block content %}
  <html lang="ja">
    <head>
      <meta charset="utf-8">
      <link rel="stylesheet" href="{% static 'bootflat/css/bootflat.min.css' %}">
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    </head>
    <body>

  <a href="{% url 'kenshinresults'%}">SEE YOUR PHOTO</a>

    <div class="container">
      <form action="{% url 'accounts:upload_save' %}" method="POST" enctype="multipart/form-data">
        {% csrf_token %}
        <p>SEND PHOTO</p>
        <input type="file" name="files[]" multiple>
        <input type="hidden" value="{{ p_id }}" name="p_id">
        <input type="submit" value="Upload">
      </form>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    </body>
  </html>

{% endblock %}

我发现只有base.html被准确显示,但是当我尝试base.htmlinherit profile.html时,发生了这个错误。以前,这两个文件是准确加载的,但是当我将href="{% static 'bootflat/css/bootflat.min.css' %}"添加到profile.html时,出现了这个错误。为什么会发生这样的错误?我怎样才能解决这个问题?我认为添加{%loadstaticfiles%}是正确的配置文件.html,但错了吗?你知道吗


Tags: httpsdivsrccombasehtml错误js
2条回答

您应该将base.html文件视为布局,将profile.html视为在该布局中呈现的模板文件。你知道吗

因此:

  • load staticfiles块应该插入到base.html中,并且应该插入到加载静态资产的每个文件中(参见下一个要点)
  • 当您在src=中引用静态资产时,就足以用static路径助手加载它
  • profile.html应该扩展布局base.html{% block content %}中包含的任何内容都将呈现在正文的block content标记内

基本.html

<html lang="ja">
  <head>
    <meta charset="utf-8">
    {% load staticfiles %}
    <link rel="stylesheet" href="{% static 'bootflat/css/bootflat.min.css' %}">
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>

  </head>
  <body>
  {% block content %} 
  <!  your body is fine  >
  {% end block %}
  </body>
</html>

配置文件.html

{% extends "registration/accounts/base.html" %}

{% block content %}
    <a href="{% url 'kenshinresults'%}">SEE YOUR PHOTO</a>
    <form action="{% url 'accounts:upload_save' %}" method="POST" enctype="multipart/form-data">
        {% csrf_token %}
        <p>SEND PHOTO</p>
        <input type="file" name="files[]" multiple>
        <input type="hidden" value="{{ p_id }}" name="p_id">
        <input type="submit" value="Upload">
    </form>  
{% endblock %}

编辑 正如丹尼尔·罗斯曼所说

该错误说明必须首先在模板中写入extends标记。 您可以在documentation中阅读更多关于此的信息

因此,您应该首先在profile.html中编写{% extends "registration/accounts/base.html" %}

{% extends "registration/accounts/base.html" %}
{% load staticfiles %}
{% block content %}
  ...
{% endblock %}

之后一切都会好起来的!你知道吗

相关问题 更多 >