首页 > 解决方案 > 无法在烧瓶中应用 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()

标签: pythonhtmlcssflask

解决方案


自己解决!原来我只是有点笨。这只是一个缓存问题,当我 ctrl-shift-r'd 时,上面的代码运行良好。


推荐阅读