I am new to flask in python. I used the url in the webview but the page said 404 Not Found. What should I do?
Send the link to your Repl.
I’m getting 500 Internal Server Error, not a 404.
Anyway, you open the file, but don’t actually set
f to that file, and so you’re trying to call the
read() method on an empty string.
So change that to this
@app.route("/") def index(): f = open("static/templates/page.html", "r") page = f.read() f.close() return page
Or better, context managers (I think that’s what they’re called?)
@app.route("/") def index(): with open("static/templates/page.html", "r") as f: page = f.read() return page
Or even better, using Flask’s built in template renderer
from flask import Flask, render_template app = Flask(__name__, template_folder="/static/templates/") @app.route("/") def index(): return render_template("page.html")
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.