【问题标题】:Setting URLS for REST API using Flask [duplicate]使用 Flask 为 REST API 设置 URL [重复]
【发布时间】:2023-03-29 11:06:02
【问题描述】:

我有两个文件 - 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。

【问题讨论】:

  • “它不工作”不是错误描述。

标签: python angularjs rest flask flask-restful


【解决方案1】:

在flask中,模板应该位于一个名为templates的目录中

您的应用应如下所示:

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

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

【讨论】:

  • 这似乎解决了这个错误。但是,它仍然无法呈现它,因为 index.html 包含一个用于输出 REST API 内容的 AngularJS 脚本。错误是“x 未定义”(我使用 x 循环通过向页面发送 GET 请求的控制器对象,从而输出 {{x.name}} 等等)。
【解决方案2】:

尝试从模板路径中删除斜线。当文件在同一目录时,则不需要。

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

【讨论】:

    猜你喜欢
    • 2014-12-13
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多