无法在烧瓶中获取 css 样式表以应用(url_for()的问题)



我正在学习一些基本教程,以便使用flask将css样式表应用于html文件。我所做的正是教程中显示的内容,但由于某些原因,样式表不适用于html。

这是主.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时,上面的代码运行良好。

最新更新