应用程序不拾取.css文件(flask / python) [英] Application not picking up .css file (flask/python)

查看:274
本文介绍了应用程序不拾取.css文件(flask / python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在渲染一个模板,我试图用外部样式表来样式。文件结构如下。

I am rendering a template, that I am attempting to style with an external style sheet. File structure is as follows.

/app
    - app_runner.py
    /services
        - app.py 
    /templates
        - mainpage.html
    /styles
        - mainpage.css

mainpage.html看起来像这样

mainpage.html looks like this

<html>
    <head>
        <link rel= "stylesheet" type= "text/css" href= "../styles/mainpage.css">
    </head>
    <body>
        <!-- content --> 

我的样式都没有应用。它有什么与事实,html是我渲染的模板吗?

None of my styles are being applied though. Does it have something to do with the fact that the html is a template I am rendering? The python looks like this.

return render_template("mainpage.html", variables..)

我知道这很多工作,因为我仍然能够渲染模板。然而,当我试图将我的样式代码从html的head标签中的样式块移动到外部文件时,所有的样式都消失了,留下一个裸html页面。任何人都看到我的文件结构的任何错误?

I know this much is working, because I am still able to render the template. However, when I tried to move my styling code from a "style" block within the html's "head" tag to an external file, all the styling went away, leaving a bare html page. Anyone see any errors with my file structure?

推荐答案

您需要有一个静态文件夹设置),除非你在Flask初始化期间专门覆盖它。我假设你没有重写它。

You need to have a 'static' folder setup (for css/js files) unless you specifically override it during Flask initialization. I am assuming you did not override it.

您的css的目录结构应如下所示:

Your directory structure for css should be like:

/app
    - app_runner.py
    /services
        - app.py 
    /templates
        - mainpage.html
    /static
        /styles
            - mainpage.css



注意你的/ styles目录应该在/ static

Notice that your /styles directory should be under /static

然后,执行此操作

<link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='styles/mainpage.css') }}">

Flask现在将在static / styles / mainpage.css

Flask will now look for the css file under static/styles/mainpage.css

这篇关于应用程序不拾取.css文件(flask / python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆