python - 外部css文件不会加载
问题描述
我正在关注 youtube 教程,但我的 CSS 不会加载。我尝试更改路径并重置 chrome 中的缓存。我不知道我可以改变什么来让它工作。其他一切都在工作我已经看过其他一些类似的帖子,但我在任何对我有用的帖子中都找不到答案。使用 VS 2019,Python 3.8
app.py
from flask import Flask, render_template, url_for
app= Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == "__main__":
app.run(debug=True)
index.html
{% extends 'base.html' %}
{% block head %}
<h1>Templated</h1>
{% endblock %}
{% block body %}
<body>Templates</body>
{% endblock %}
base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" type="text/css" herf="{{ url_for('static', filename='/css/main.css') }}" >
{% block head %}{% endblock %}
</head>
<body>
{% block body %}
<link rel="stylesheet" type="text/css" herf="{{ url_for('static', filename='/css/main.css') }}">
{% endblock %}
</body>
</html
main.css
body {
color: blue;
margin: 0;
font-family: sans-serif;
}
h1 {
color: red;
margin: 0;
font-family: sans-serif;
}
路径:C:\Users\username\OneDrive\Programming\Visual Studio 2019\Flask Introduction\static\css\main.css
解决方案
在您的base.html和link
标签中,删除/
路径中的第一个/css/main.css
( => css/main.css
) 并更改为此并检查它是否有效:
<link rel="stylesheet" type="text/css" herf="{{ url_for('static', filename='css/main.css') }}">
你不应该使用relative path
那个absolute
。
推荐阅读
- ios - swiftui 在长按消息应用程序后将视图带到中心并模糊背景
- reactjs - 从 React Router v6 Navigate 检索状态
- r - R Shiny:有没有办法在用户输入中读取单个反斜杠?
- python - 我将如何测量/记录 keras / tensorflow 的人工神经网络算法的总训练时间?
- reactjs - 如何将地理位置传递给传单?
- php - 如何在表格中自动显示计数数据累积和残差数据?
- python - 为什么 python 2d 列表彼此共享它们的元素?
- ros - Rosbag 记录不记录第一条消息
- python - 特征归一化后识别目标对象的最佳算法是什么?
- c - 如何通过管道发送不同数量的数据并测量它们之间的执行时间?