[英]Python Flask not displaying contents with render_template()
當我嘗試運行我的 Flask 程序時,我看到的只是背景顏色。 在瀏覽器輸入localhost:5000/index
,沒有顯示HTML的內容。
我的文件夾結構是這樣的:
Flask
---測試.py
- -靜止的
------樣式
----------主要.css
---模板
------基地.html
------家.html
---緩存
代碼如下:
測試.py
from flask import Flask
from flask import render_template
from datetime import datetime
app = Flask(__name__)
@app.route("/")
@app.route("/home/")
def home(name):
return render_template("home.html", name=name)
if __name__ == "__main__":
app.run(debug=True)
家.html:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ name }}</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles/main.css') }}">
</head>
<body>
<div id="container">
<!-- Jinja directives: page contents will go between them -->
{% block content %}
{% endblock %}
</div>
</body>
</html>
家.html
{% extends 'base.html' %}
{% block body %}
<h1>{{ name }}</h1>
{% endblock %}
主要.css
body {
font-family: 'Source Sans Pro', sans-serif;
font-size: 100%;
color: #000;
background: #407189;
margin: 0;
}
#container {
margin: 2em auto;
width: 740px;
padding: 2em 4em;
background: #f1f7dc;
}
h1, h2 {
font-family: 'Droid Serif', serif;
font-size: 3em;
margin: 0;
padding-bottom: 1em;
}
h2 {
font-size: 2em;
}
p {
font-size: 1.4em;
}
@media (max-width: 800px) {
body {
background: #f1f7dc;
}
#container {
margin: 0;
width: 100%;
padding: 2em;
}
}
在base.html 中將{% block content %}
更改為{% block body %}
<div id="container">
<!-- Jinja directives: page contents will go between them -->
{% block body %}
{% endblock %}
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.