Flask找不到我的包的'static'目录中的文件 [英] Flask not finding files in my package's 'static' directory

查看:739
本文介绍了Flask找不到我的包的'static'目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于这个网站上的其他几个人,但我发现的答案对我来说不起作用。



我正在学习Flask。我在一个Ubuntu 10.04机器上使用Vagrant VM运行Flask 0.10.1和Python 2.7。



我试过无数的建议,从S​​O,Flask文档,

因此,我展示了我可以可以工作的最简单的版本(但不是)。



注释掉的替换行(在page_a.html和page_b.html中) 有效,但是可怕。

首先,这是项目根目录下的'tree'输出:





这里是文件(减去一些在.html中的样板)



page_a.html: / p>

 < head> 
< link rel =stylesheethref =styles.css>
<! - < link rel =stylesheethref =static / styles.css> - >
< / head>
< body>
< h1> page_a< / h1>
< img src =an_image.png>
<! - < img src =static / an_image.png> - >
< a href ={{url_for('page_b')}}>到第b页< / a>
< / body>

page_b.html:

 < head> 
< link rel =stylesheethref =styles.css>
<! - < link rel =stylesheethref =../../ static / styles.css> - >
< / head>
< body>
< h1> page_b< / h1>
< img src =../../ static / an_image.png>
<! - < img src =../../ static / an_image.png> - >
< a href ={{url_for('page_a')}}>到页面< / a>
< / body>

init .py:

  from flask import Flask 
app = Flask('my_pages')
import my_pages.views

runserver.py:

  from my_pages import app 
app.run(host ='0.0.0.0',port = 5000,debug = True)

views.py:

  from my_pages import app 
from flask import render_template

@ app.route('/')
@ app.route('/ page_a')
def page_a():
return render_template('page_a.html')

@ app.route('/ pages / page_b /')
def page_b():
return render_template('page_b.html')
/ pre>

styles.css:

  body {
background-color:green;
}



这是runserver的输出。 py当我访问page_a.html:

 GET / page_a HTTP / 1.1200  -  
GET / styles .css HTTP / 1.1404 -
GET /an_image.png HTTP / 1.1404-

和page_b.html。

 GET / pages / page_b / HTTP / 1.1200  -  
GET /pages/page_b/styles.css HTTP / 1.1404 -

(最后显示an_image .png')。



我的问题:我缺少什么?这个设置可以在没有重大重构的情况下工作吗?



我当然不想硬编码每个静态文件的完整路径。



此外,在实际应用中,URL运行几个级别,例如

  http:// localhost:5000 / book /< id_1> / chapter /< id_2> / page 


$ b b

非常感谢任何可能回复的人。

解决方案

你不是告诉Flask文件是在你的静态夹。最简单的方法是使用 url_for

 < link rel =stylesheethref ={{url_for('static',filename ='styles.css')}}> 


My question is similar to several others on this site, but the answers I find there aren't working for me.

I'm learning Flask. I'm running Flask 0.10.1 and Python 2.7, on an Ubuntu 10.04 machine using a Vagrant VM.

I've tried countless suggestions from SO, the Flask docs, and Miguel Grinberg, without success.

So I'm showing the simplest version that I thought could work (but doesn't) for your perusal.

The commented-out substitute lines (in page_a.html and page_b.html) do work but are hideous.

First, here's the output of 'tree' on my project root:

And here are the files (minus a bit of boilerplate in the .html)

page_a.html:

<head>
    <link rel="stylesheet" href="styles.css">
    <!-- <link rel="stylesheet" href="static/styles.css"> -->
</head>
<body>
    <h1>page_a</h1>
    <img src="an_image.png">
    <!-- <img src="static/an_image.png"> -->
    <a href="{{ url_for('page_b') }}">to page b</a>
</body>

page_b.html:

<head>
    <link rel="stylesheet" href="styles.css">
    <!-- <link rel="stylesheet" href="../../static/styles.css"> -->
</head>
<body>
    <h1>page_b</h1>
    <img src="../../static/an_image.png">
    <!-- <img src="../../static/an_image.png"> -->
    <a href="{{ url_for('page_a') }}">to page a</a>
</body>

init.py:

from flask import Flask
app = Flask('my_pages')
import my_pages.views

runserver.py:

from my_pages import app 
app.run(host='0.0.0.0', port=5000, debug=True)

views.py:

from my_pages import app 
from flask import render_template

@app.route('/')
@app.route('/page_a')
def page_a():
    return render_template('page_a.html')

@app.route('/pages/page_b/')
def page_b():
    return render_template('page_b.html')

styles.css:

body {
    background-color: green;
}

This version does work when, in page_a.html and page_b.html, I use the commented-out lines (instead of the lines above them).

Here's the output of runserver.py when I access page_a.html:

"GET /page_a HTTP/1.1" 200 -
"GET /styles.css HTTP/1.1" 404 -
"GET /an_image.png HTTP/1.1" 404 -

and page_b.html.

"GET /pages/page_b/ HTTP/1.1" 200 -
"GET /pages/page_b/styles.css HTTP/1.1" 404 -

(this last shows 'an_image.png' from my 'styles' directory)

My questions: What am I missing? Can this setup be made to work without a major refactoring?

I of course don't want to hard-code the full path to every static file.

Also, in the real application, the URLs run several levels deep -- e.g.,

http://localhost:5000/book/<id_1>/chapter/<id_2>/page

Many thanks to anyone who might reply!

解决方案

You aren't telling Flask that the files are in your static folder. The easiest way to do that is with url_for.

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

这篇关于Flask找不到我的包的'static'目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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