Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

python - i am trying to extend a html file in flask but its not reflecting in output?

I am new to Flask. I started from some Youtube videos,but somehow mine is not working with the extends part. I am getting the output as if I haven't extended the second HTML file specified below. Am I missing something ?

This is my "main.py" file

from flask import Flask,redirect,url_for,render_template

app = Flask(__name__)


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


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

this is "templates/home.html" file

<!doctype html>
<html lang="en">
  <head>
    <title>mY Page</title>
  </head>
  <body>
    <h1> this is nav----------- bar</h1>
    <hr/>
    {% include 'templates/test.html' %}
    <div id="content">{% block content %}{% endblock %}</div>
    
  </body>
</html>

and this is 2nd html "templates/test.html"

{% extends 'templates/home.html' %}
{% block content %}
  <h1>Index</h1>
  <p class="important">
    Welcome on my awesome homepage.
{% endblock %}
question from:https://stackoverflow.com/questions/65867984/i-am-trying-to-extend-a-html-file-in-flask-but-its-not-reflecting-in-output

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Probably the templates/ part is not required for extends.

Just use: {% extends 'home.html' %}

in place of: {% extends 'templates/home.html' %}

and it should work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...