Python 在 jinja2 模板上使用 css 样式表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25034812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
use a css stylesheet on a jinja2 template
提问by ptolemy0
I am making a website using html, css, flask and jinja2.
我正在使用 html、css、flask 和 jinja2 制作网站。
I have a page working on a flask server, the buttons and labels etc. are displayed, but the css stylesheet I have is not loaded in.
我有一个在 Flask 服务器上工作的页面,显示了按钮和标签等,但没有加载我拥有的 css 样式表。
How would I link a stylesheet to a jinja2 template. I have looked around on the internet but cannot find out how.
我如何将样式表链接到 jinja2 模板。我在互联网上环顾四周,但无法找到方法。
Here is the css stylesheet link; should I change this, or the python code?
这是 css 样式表链接;我应该改变这个,还是python代码?
<link rel="stylesheet" type="text/css" href="styles.css">
<link rel="stylesheet" type="text/css" href="styles.css">
here is my flask code:
这是我的烧瓶代码:
@app.route('/')
def resultstemplate():
return render_template('questions.html', head='Welcome!')
here are the locations of the files:
以下是文件的位置:
/python-code.py
/templates/template.html
/templates/styles.css
/python-code.py
/templates/template.html
/templates/styles.css
采纳答案by Andrejs Cainikovs
All public files (the ones that are not processed, like templates or python files) should be placed into dedicated static folders. By default, Jinja2 has one static folder called static.
所有公共文件(未处理的文件,如模板或 python 文件)都应放在专用的静态文件夹中。默认情况下,Jinja2 有一个名为static.
This should fix your problem:
这应该可以解决您的问题:
Move
/templates/styles.cssto/static/styles.cssUpdate your code with following code, that will be translated into correct file location:
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
移动
/templates/styles.css到/static/styles.css使用以下代码更新您的代码,该代码将被转换为正确的文件位置:
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
More info on static files in Jinja2 is here.
关于 Jinja2 中静态文件的更多信息在这里。
回答by Sunil B N
<link rel="stylesheet" type="text/css" href="styles.css">
href value must be within quotes.
href 值必须在引号内。
make sure the file name and path are proper OR try the below
确保文件名和路径正确或尝试以下
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}"/>
回答by George Nguyen
The order of handler might cause the problems:
处理程序的顺序可能会导致问题:
url: /stylesheets static_dir: stylesheets
url: /.* script: helloworld.application
will work instead of
将工作而不是
url: /.* script: helloworld.application
url: /stylesheets static_dir: stylesheets
回答by wcyn
Tried almost every solution on Stack Overflow. It only worked for me when I placed the staticfolder in the samedirectory as my run.pyfile.
I changed my folder structure from:
在 Stack Overflow 上尝试了几乎所有的解决方案。当我将static文件夹与我的文件放在同一目录中时,它只对我run.py有用。我改变了我的文件夹结构:
app/
views
static
templates
run.py
To:
到:
app/
views
templates
static
run.py
I guess moving the run.pyinstead would work too. Have a look at this Jinja Templating Tutorialfor extra info. Not sure why I had to change the structure for it to work though.
我想移动run.py代替也会起作用。查看此Jinja 模板教程以获取更多信息。不知道为什么我必须改变结构才能让它工作。
回答by jaredgorski
To add to what's been said here, be sure to update Jinja2 to v2.10, as lesser versions seem to cause this same bug. Cheers!
要补充这里所说的内容,请确保将 Jinja2 更新到 v2.10,因为较小的版本似乎会导致相同的错误。干杯!

