简体   繁体   English

在Flask应用中调用函数时出现404错误

[英]404 error when calling function in Flask app

I am loading data from a JSON file and returning a template with this function in Flask: 我正在从JSON文件加载数据并在Flask中使用此函数返回模板:

import json
def template():
    with open(PERSONAL_DATA, "r") as info:
        info = json.load(info)
    return render_template("temp.html", title="Info",
                           header=str(info[name])) event you have.")

But when I call this function from my app, I get a 404 page. 但是,当我从应用程序中调用此函数时,会得到一个404页面。 The function is in the main file, and it runs fine on it's own. 该函数位于主文件中,并且可以独立运行。

You didn't wrap the function in an app route decorator. 您没有将函数包装在应用程序路由装饰器中。 See this Flask URL registration documentation for more info. 有关更多信息,请参见此Flask URL注册文档

@app.route("/template", methods=["GET", "POST"])
def template():
    with open(PERSONAL_DATA, "r") as info:
        info = json.load(info)
    return render_template("temp.html", title="Info",
                           header=str(info[name]))

Also, import json at the top of the file, not right before you use it. 另外,请在文件顶部导入json,而不是在使用之前导入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM