使用Flask为REST API设置URLS

时间:2016-03-09 10:09:53

标签: python angularjs rest flask flask-restful

我有两个文件 - app.py包含REST API的代码,另一个页面index.html使用AngularJS显示API的一些内容。目前,我必须从浏览器打开index.html文件以使其正常工作(在部署服务器http://localhost:5000之后)。

我试过

@app.route('/')
def index():
return render_template('/index.html',
                       title='Home')

它给出错误“TemplateNotFound:/index.html”。目前,app.py和index.html位于同一目录中。如何更改格式以让Flask知道它必须在此URL上呈现页面index.html。

2 个答案:

答案 0 :(得分:2)

在flask中,模板应位于名为templates

的目录中

您的应用应该如下:

./app.py
./templates/index.html

http://flask.pocoo.org/docs/0.10/tutorial/templates/将以下模板放入模板文件夹

答案 1 :(得分:0)

尝试从模板路径中删除斜杠。当文件在同一目录中时,没有必要。

return render_template('index.html',
                   title='Home')