Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 在Flask应用程序中保存js文件的位置?_Angularjs_Flask_Src - Fatal编程技术网

Angularjs 在Flask应用程序中保存js文件的位置?

Angularjs 在Flask应用程序中保存js文件的位置?,angularjs,flask,src,Angularjs,Flask,Src,我不熟悉flask,并使用它在“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 sr

我不熟悉flask,并使用它在“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>


但它并没有同时包含这两个文件(我从“查看页面源”中检查了它们)。是否存在flask从中获取包含文件的默认目录?如何实现这一点?

我刚刚解决了这个问题,在app.py中添加了两个@app.route,分别为angular.js和app.js文件提供服务。我用了这些路线。但我觉得这不是正确的方法。我不知道。

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

<script src="{{ url_for('static', filename='angular.js')}}"</script>
另外,创建名为“templates”的目录,将所有html文件复制到其中,并在方法中使用render_template方法,如下所示:

return render_template("index.html")
因此,您的目录结构应该如下所示:

app.py
static --> contains all static files
templates --> contains all html files
链接:


希望有帮助。

回答得好。对于OP,不要忘记,在生产环境中,您应该将Web服务器配置为直接为静态文件提供服务,而不是将这些请求传递给Flask。Web服务器在提供静态内容方面比Flask要高效得多。
app.py
static --> contains all static files
templates --> contains all html files