Home > database >  python Flask does not inherit template
python Flask does not inherit template

Time:08-17

i am learning the template inheritance. I was trying to do it but does not work. i tried; changing location of the file, name of the file, the code itself but still not working. Other flask properties works fine.

Deneme.py:

from flask import Flask, render_template, redirect, url_for

app = Flask(__name__)

@app.route("/")
def home():
    return render_template("go.html")

if __name__ == "__main__":
    app.run(debug=True)

go.html:

<!DOCTYPE html>
<html lang="en">
  <head>
      <title>Template Inheritance</title>
  </head>
  <body>
      <h1> This heading is common in all webpages </h1>
      {% block content %}
      {% endblock %}

  </body>
</html>

side.html:

{% extends "go.html" %}
{% block content %}
  
<h1>Welcome to Home</h1>
  
{% endblock %}

output of the code

both html files are in the template folder. i think it is not about code. what is wrong?

CodePudding user response:

The problem is in this line of code of your view function:

return render_template("go.html")

You are returning go.html. You need to return side.html which extends the go.html file. Make it:

return render_template("side.html")
  • Related