Issue
I get the following error when I try to run my app.
File "/Users/ccc/microblog/app/templates/errors/500.html", line 1, in top-level template code
{% extends "base.html" %}
File "/Users/ccc/microblog/app/templates/base.html", line 48, in top-level template code
{% block scripts %}
File "/Users/ccc/microblog/app/templates/base.html", line 49, in block "scripts"
{{ super() }}
jinja2.exceptions.UndefinedError: there is no parent block called 'scripts'.
I am following Miguel's Flask lessons and it seems like our code is largely similar. Hence I am not too sure this error appears. This does not happen when I remove {{super()}} from the block script. The code runs smoothly then.
Here is my code for the 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/[email protected]/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>
Solution
The error message said it right: you cannot use super()
in your base.html
template since it does not have a parent template. super()
can be used only in a child template. When you put {{ super() }}
into a block in a child template it will include the block's content from the parent template. E.g. if you want to add an additional script in the child.html
you can write:
{% extends "base.html" %}
{% block scripts %}
{{ super() }}
<script src="https://cnd.com/path.to.script.js></script>
{% endblock %}
Now it will include all scripts from the parent and this one as well.
Answered By - Dauros
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.