4

我是烧瓶的新手,并使用它在“localhost:5000/”处提供 index.html。目前我只有 3 个文件:index.html、angular.js 和 app.js;并且它们都在同一个文件夹中。我将 index.html 服务为:

@app.route('/')
def index():
    return make_response(open('index.html').read())

我想在 index.html 中包含 angular.js 和 app.js。我试过这个:

<script src="angular.js"></script>
<script src="app_controller.js"></script>

但它不包括这两个文件(我从“查看页面源”中检查过)。烧瓶是否有默认目录从其中获取包含文件?我应该如何实现这一目标?

4

2 回答 2

10

Flask 应该知道它可以提供静态文件的路径。创建“静态”目录,将所有静态文件放入其中。并且,在 index.html 中使用:

<script src="{{ url_for('static', filename='angular.js')}}"</script>

请注意,有一个名为“static”的特殊端点,它查看名为“static”的目录并为该文件提供服务。您还可以更改静态目录的名称。

app = Flask(__name__, static_folder="differet_dir_name")

此外,创建名为“模板”的目录并将所有 html 文件复制到其中,并在方法中使用 render_template 方法:

return render_template("index.html")

因此,您的目录结构应如下所示:

app.py
static --> contains all static files
templates --> contains all html files

链接:http : //flask.pocoo.org/docs/quickstart/#static-files http://flask.pocoo.org/docs/api/#application-object

希望能帮助到你。

于 2013-09-20T05:36:37.577 回答
0

我刚刚通过向 app.py 添加另外两个 @app.route 来解决它,分别为文件 angular.js 和 app.js 提供服务。我在 . 但我不认为这是正确的做法。我不知道。

于 2013-09-20T05:02:47.577 回答