简体   繁体   English

为什么Flask加载不存在的CSS styles?

[英]Why is Flask loading CSS styles that don't exist?

I am trying to make my first website.我正在尝试制作我的第一个网站。 I am using Flask with HTML, CSS and Bootstrap.我将 Flask 与 HTML、CSS 和引导程序一起使用。 Once I am happy with the front-end I am planning to host it on a 2010 iMac running something like Ubuntu Server and Apache or Nginx.一旦我对前端感到满意,我计划将它托管在 2010 年 iMac 上,运行类似 Ubuntu 服务器和 Apache 或 Z62E0B5B350C9E3D2C19AA801D94429 之类的东西。

The current issue that I am having is that, since I moved the project folder to a different directory, the design of the page does not match css stylesheet.我当前遇到的问题是,由于我将项目文件夹移动到不同的目录,因此页面的设计与 css 样式表不匹配。 At first I wasn't sure if it was my poor CSS skills or the computer, to test I added dramatic changes such as:起初我不确定是我糟糕的 CSS 技能还是计算机,为了测试我添加了一些戏剧性的变化,例如:

* {
color: red;
}

Nothing changed when I did this the other day, but when I opened the project today the above style was applied even though it no longer exists in the stylesheet.前几天我这样做时没有任何改变,但是当我今天打开项目时,应用了上述样式,即使它不再存在于样式表中。 In fact nothing in the stylesheet is set to red.事实上,样式表中的任何内容都没有设置为红色。 Because none of the changes that I was making to style.css were being applied I recently I removed the stylesheet from /static , however styles are still being applied to the page (including all the font being the color red).因为我对style.css的任何更改都没有应用,所以我最近从/static中删除了样式表,但是 styles 仍在应用到页面(包括所有字体为红色)。 Why is this?为什么是这样?

Possible causes: The program seems to be searching for the stylesheet in /templates : <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}"> I discovered this by control+click on {{ url_for('static', filename='style.css') }} to try and go to the location of the file VSCode gives me this error可能的原因:程序似乎在/templates中搜索样式表: <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">我通过控制+点击{{ url_for('static', filename='style.css') }}来尝试和 go 到文件的位置发现了这个 VSCode 给了我这个错误

Unable to read file `'/project/templates/{{ url_for('static', filename='style.css') }}` Unable to resolve non-existing file.`

The above error occurs regardless of whether style.css is in /static or not.无论style.css是否在/static中,都会出现上述错误。

In application.py I have tried to rectify this:application.py我试图纠正这个:

app = Flask(__name__,
            static_folder='/static')

Also I have read on other posts that it is considered better practice to load the stylesheet from the Apache/Nginx.此外,我在其他帖子上读到,从 Apache/Nginx 加载样式表被认为是更好的做法。 Could anyone explain why this is or link me to a resource that explains this in lanugage understandable to a beginner?谁能解释为什么会这样,或者将我链接到一个资源,该资源以初学者可以理解的语言解释这一点?

Thanks in advance,提前致谢,

Basic answer is you have to set static_url_path here app = Flask(__name__, static_url_path="/STATIC_FOLDER", static_folder='STATIC_FOLDER')基本答案是你必须在这里设置static_url_path app = Flask(__name__, static_url_path="/STATIC_FOLDER", static_folder='STATIC_FOLDER')

About nginx : you don't need nginx for local development, it is used in deployment step when you or some devops link specific domain name with your apps关于nginx :您不需要 nginx 进行本地开发,当您或某些开发人员将特定域名与您的应用程序链接时,它用于部署步骤

shift + f5 clears the cache for CSS styling on some browsers. shift + f5在某些浏览器上清除 CSS 样式的缓存。 This is opposed to just refreshing the page.这与仅刷新页面相反。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM