【发布时间】:2020-10-18 01:43:24
【问题描述】:
我正在学习一些基本教程,以获取 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()
【问题讨论】:
-
在花括号之间添加一些空格,例如
href="{{ url_for('static', filename='css/main.css') }}">看看是否可行。如果不是,请确保您的静态根目录实际上是正确的,例如如果您的静态设置为/home/something,并且您的文件基于css/main.css,则实际路径应为/home/something/css/main.css