python - 无法在烧瓶中应用 css 样式表(url_for() 问题)
问题描述
我正在按照一些基本教程来获取一个 css 样式表,以使用烧瓶应用于 html 文件。我所做的正是教程所显示的,但由于某种原因,样式表不适用于 html。
这是 main.css 文件:
body {
margin: 50px;
font-family: sans-serif;
}
这是 base.html 文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{url_for('static', filename='css/main.css')}}">
<title>Document</title>
{% block head %}{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>
这是我的 index.html 文件:
{% extends 'base.html' %}
{% block head %}
{% endblock %}
{% block body %}
<h1>Image Upload</h1>
<form id="upload form" action="{{ url_for('upload') }}" method="POST" enctype="multipart/form-data">
<input type="file" name="file" accept="image/*">
<input type="submit" value="Upload">
</form>
{% endblock %}
如果需要,这是 app.py 文件:
import os
from flask import Flask, render_template, url_for, request, redirect
app = Flask(__name__)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
@app.route("/")
def index():
"""Landing page. Has an image upload button."""
return render_template("index.html")
@app.route("/upload", methods=["POST"])
def upload():
"""Page displayed after an image is uploaded. Currently it just displays the image and has a button to go back to the landing page."""
target = os.path.join(APP_ROOT, 'static/images/')
print(target)
if not os.path.isdir(target):
os.mkdir(target)
for file in request.files.getlist("file"):
print(file)
filename = file.filename
destination = "/".join([target, filename])
print(destination)
file.save(destination)
return render_template("uploaded.html", image_name=filename)
if __name__ == "__main__":
app.run()
解决方案
自己解决!原来我只是有点笨。这只是一个缓存问题,当我 ctrl-shift-r'd 时,上面的代码运行良好。
推荐阅读
- javascript - 如何验证 JsonWebtoken 错误“SyntaxError: Unexpected token n JSON at position 75”?
- docker - 没有密码的用户无法在 Docker 中共享本地驱动器
- xamarin.android - 解决失败:[Lcom/google/android/gms/phenotype/ExperimentTokens,当我点击地点搜索栏时
- c# - 如何从 SpringCM 工作流中获取 Salesforce 字段?
- reactjs - 使用 React.lazy、Suspense 和 react-router-dom 进行代码拆分不起作用
- sql - asp.net rdlc 报告
- javascript - 在大数据上调用 fromCharCode 的有效方法
- javascript - 如何消除不想要的位置间隙:粘性
- r - 升级适用于 Windows 的机器学习服务器的 R 版本
- mysql - 如何使光标工作并填写列表,程序唯一参数的一行中的值