简体   繁体   中英

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:

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. 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.

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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