Home > OS >  Flask jinja2.exceptions.TemplateSyntaxError while inheriting
Flask jinja2.exceptions.TemplateSyntaxError while inheriting

Time:05-24

I am using flak and jinja2 to template inheritance. I get this error.

jinja2.exceptions.TemplateSyntaxError: Unexpected end of template. Jinja was looking for the following tags: 'endblock'. The innermost block that needs to be closed is 'block'.

The python code

from flask import *

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/bear.html')
def bear():
   return render_template('bear.html')


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=2092)

The Html code base

<!DOCTYPE html>
<html>
    <head>
      <style>
        table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
      </style>

        <link rel="stylesheet" href="index.css">
        <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X R7YkIZDRvuzKMRqM OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
        <nav >
            <div >
              <a  href="index.html"><img src="book.svg"/>   Encyclopedia</a>
              <button  type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span ></span>
              </button>
              <div  id="navbarSupportedContent">
                <ul >
                
                  <li >
                    <a  href="#">Link</a>
                  </li>
                </ul>
              </div>
            </div>
          </nav>
    </head>
    <body>
        <div >   
          
        {% block img %}
        {% endblock %}
        <P>
          KINGDOM
          {% block kingdom %}
          {% endblock %}
          <br>
          PHYLUM
          {% block phylum %}
          {% endblock %}
          <br>
          SUBPHYLUM
          {% block subphylum %}
          {% endblock %}  
          <br>
          CLASS
          {% block class %}
          {% endblock %}
          <br>
          ORDER
          {% block order %}
          {% endblock %}
          <br>
          SUBORDER
          {% block suborder %}
          {% endblock %}
          <br>
          FAMILY
          {% block family %}
          {% endblock %}
          <br>
          GENUS 
          {% block genus %}
          {% endblock %}
          <br>
          SPECIES
          {% block species %}
          {% endblock %}
        </P>
    </div>
        
        
      
        <table>
          <tr>
            <th>Information</th>
            <th></th>
            
          </tr>
          <tr>
            <td>Population size</td>
            
            <td>lifespan</td>
            
            <td>Top Speed</td>
            
            <td>Weight</td>
            
            <td>Height</td>
            
            <td>Length</td>
          </tr>
          <tr>

            <td>{% block population %}
                {% endblock %}</td>
            
            <td>{% block lifespan %}
                {% endblock %}</td>
            
            <td>{% block topspeed %}
                {% endblock %}</td>
            
            <td>{% block weight %}
                {% endblock %}</td>
            
            <td>{% block height %}
                {% endblock %}</td>
            
            <td>{% block length %}
                {% endblock %}</td>
            
          </tr>
          
        </table>
        <p>{% block body %}
           {% endblock %}</p>
        <P>CONTINENTS
          {% block continents %}
          {% endblock %}
          <br>
          SUBCONTINENTS
          {% block subcontinents %}
          {% endblock %}
          <br>
          COUNTRIES
          {% block countries %}
          {% endblock %}
          <br>
          BIOGEOGRAPHICAL REALMS
          {% block realms %}
          {% endblock %}
        </P>
        
    </body>
</html>

The second HTML

{% extends "base.html" %}
{% block img %}
<img src="https://animalia.us-east-1.linodeobjects.com/animals/photos/full/1.25x1/DQjvOrjwumEpEyBEaJtX.jpg">
{endblock}
{% block kingdom %}
Animalia
{% endblock %}
{% block phylum %}
Chordata
{% endblock %}
{% block subphylum %}
Vertebrata
{% endblock %}
{% block class %}
Mammalia
{% endblock %}
{% block order %}
Carnivora
{% endblock %}
{% block suborder %}
Mammalia
{% endblock %}
{% block family %}
Ursidae
{% endblock %}
{% block genus %}
Ursus
{% endblock %}
{% block species %}
Ursus arctos
{% endblock %}
{% block population %}
200 000
{% endblock %}
{% block lifespan %}
20-50 Years
{% endblock %}
{% block topspeed %}
56 KM/H
{% endblock %}
{% block weight %}
100-635 KG
{% endblock %}
{% block height %}
70-153 cm
{% endblock %}
{% block length %}
1.4-2.8 M
{% endblock %}
{% block body %}
The Brown bear is a large mammal with a notable hump of muscles over its shoulders. This animal is the second largest species of bear. The legs of Brown bear are strong with huge paws. Their claws are rather long on their front feet, allowing them to dig their dens as well as dig for food. The ears are relatively small and the face is concave while the head is large with powerful jaws. Brown bears have ability of standing and walking on their hind legs; they do so in order to determine location of a food source or to identify a threat. These animals have thick coat, varying in color from black to brown and blonde. The guard hair of these animals is longer, sometimes having white tip, which gives them grizzled appearance.
{% endblock %}
{% block continents %}
Asia, Europe, North America
{% endblock %}
{% block subcontinents %}
South Asia, Western Asia, East Asia, Central Asia, Southeast Asia
{% endblock %}
{% block countries %}
Albania, Armenia, Austria, Azerbaijan, Belarus, Bosnia and Herzegovina, Bulgaria, Canada, China, Croatia, Estonia, Finland, France, Georgia, Greece, India, Iran, Iraq, Italy, Japan, Kazakhstan, North Korea, Kyrgyzstan, Latvia, Macedonia, Mongolia, Montenegro, Nepal, Norway, Pakistan, Poland, Romania, Russia, Serbia, Slovakia, Slovenia, Spain, Sweden, Tajikistan, Turkey, Ukraine, United States, Uzbekistan, Bhutan, Lebanon, Afghanistan
{% endblock %}

CodePudding user response:

In the second line of the second html you have an endblock like this {endblock} This may be causing the error because it's a syntax error. Modify it to {% endblock %} and it should be good to go!

CodePudding user response:

This is a syntax error. In the forth line of second html, you forgot to put % at both ends: Instead of this {% endblock %}, you wrote {endblock}

  • Related