Python jinja2.exceptions.UndelineError:没有名为';脚本';

Python jinja2.exceptions.UndelineError:没有名为';脚本';,python,flask,jinja2,Python,Flask,Jinja2,当我尝试运行我的应用程序时,出现以下错误 File "/Users/chuangzhequan/microblog/app/templates/errors/500.html", line 1, in top-level template code {% extends "base.html" %} File "/Users/chuangzhequan/microblog/app/templates/base.html", line 48, in top-level templa

当我尝试运行我的应用程序时,出现以下错误

  File "/Users/chuangzhequan/microblog/app/templates/errors/500.html", line 1, in top-level template code
    {% extends "base.html" %}
  File "/Users/chuangzhequan/microblog/app/templates/base.html", line 48, in top-level template code
    {% block scripts %}
  File "/Users/chuangzhequan/microblog/app/templates/base.html", line 49, in block "scripts"
    {{ super() }}
jinja2.exceptions.UndefinedError: there is no parent block called 'scripts'.
我正在学习Miguel的Flask课程,看起来我们的代码在很大程度上是相似的。因此,我不太确定是否会出现此错误。 从块脚本中删除{{super()}}时不会发生这种情况。代码运行平稳

下面是我的base.html代码:

<head>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>

<body>
  {% block navbar %}
   <nav class="navbar  navbar-expand-lg navbar-dark bg-dark">
       <div class="container">
           <div class="navbar-header">
               <a class="navbar-brand" href="{{ url_for('main.index') }}">Microblog</a>
           </div>
           <div class="collapse navbar-collapse" id="navbarSupportedContent">
               <ul class="navbar-nav mr-auto">
                   <li><a class="nav-link" href="{{ url_for('main.index') }}">Home</a></li>
                   <li><a class="nav-link" href="{{ url_for('main.explore') }}">Explore</a></li>
                   <li><a class="nav-link" href="{{ url_for('main.add_habit') }}">Habits</a></li>
               </ul>
               <ul class="nav navbar-nav navbar-right">
                   {% if current_user.is_anonymous %}
                   <li><a class="nav-link" href="{{ url_for('auth.login') }}">Login</a></li>
                   {% else %}
                   <li><a class="nav-link" href="{{ url_for('main.user', username=current_user.username) }}">Profile</a></li>
                   <li><a class="nav-link" href="{{ url_for('auth.logout') }}">Logout</a></li>
                   {% endif %}
               </ul>
           </div>
       </div>
       <hr>
   </nav>
  {% endblock %}

  {% block content %}
    <div class="container">
      {% with messages = get_flashed_messages() %}
      {% if messages %}
        {% for message in messages %}
        <div class="alert alert-secondary" role="alert">{{ message }}</div>
          {% endfor %}
          {% endif %}
          {% endwith %}

        {# application content needs to be provided in the app_content block #}
        <br>
        {% block app_content %}{% endblock %}
    </div>
  {% endblock %}

  {% block scripts %}
    {{ super() }}
    {{ moment.include_jquery() }}
    {{ moment.include_moment() }}
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
  {% endblock %}
</body>

{%block navbar%}
    {%如果当前_user.is_anonymous%}
  • {%else%}
  • {%endif%}

{%endblock%} {%block content%} {%with messages=get_flashed_messages()%} {%if消息%} {消息%中的消息为%s} {{message}} {%endfor%} {%endif%} {%endwith%} {#应用程序内容需要在应用程序内容块中提供#}
{%block app_content%}{%endblock%} {%endblock%} {%block scripts%} {{super()}} {{moment.include{jquery()}} {{时刻。包括{u时刻()}} {%endblock%}
错误消息说得对:您不能在
base.html
模板中使用
super()
,因为它没有父模板<可以使用code>super()。将
{{super()}}
放入子模板中的块时,它将包括父模板中的块内容。例如,如果要在
child.html
中添加其他脚本,可以编写:

{% extends "base.html" %}

{% block scripts %}
  {{ super() }}
  <script src="https://cnd.com/path.to.script.js></script>
{% endblock %}
{%extends“base.html”%}
{%block scripts%}
{{super()}}

错误消息说得对:您不能在
base.html
模板中使用
super()
,因为它没有父模板<可以使用code>super()
。将
{{super()}}
放入子模板中的块时,它将包括父模板中的块内容。例如,如果要在
child.html
中添加其他脚本,可以编写:

{% extends "base.html" %}

{% block scripts %}
  {{ super() }}
  <script src="https://cnd.com/path.to.script.js></script>
{% endblock %}
{%extends“base.html”%}
{%block scripts%}
{{super()}}

这回答了你的问题吗?这回答了你的问题吗?