我怎么能在我的html中显示CSS动画使用flask?

dly7yett  于 2022-12-09  发布在  其他
关注(0)|答案(1)|浏览(109)

im a begginer here , struggling with basic stuff
I have my flask:

from flask import Flask , render_template , send_file , send_from_directory 

import os
app = Flask(__name__)

#PRIMERA FUNCION
@app.route('/')
def index():
    return  render_template('prueba.html')

and i have my html :

<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title>CodePen - The Arrow</title>
  <link rel="stylesheet" href="./style.css">

</head>
<body>
<!-- partial:index.partial.html -->
<div class="arrow-1"></div>

<!-- partial -->
  
</body>
</html>

when I open the html file on browser it shows the css , but when I run flask css doesnt show and I cant figure out why!!!
I have try this

@app.route('/css/<path:filename>')
def css_file(filename):
    file_path = os.path.join('css', filename)
    return send_file(file_path)

and i also thought it was a problem of my folder layout but I already tried re arrange folders

f0ofjuux

f0ofjuux1#

Flask有不同的方法来实现这一点,但惯例是将静态资产(如CSS)放在static目录(应该在项目的根目录)中,并使用Jinja和url_for()函数将它们链接起来。因此,在您的情况下,它看起来如下所示:

<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title>CodePen - The Arrow</title>
  <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>

Flask docs中有详细说明

相关问题