Search code examples
pythontemplatesflaskdirectory

Where do I put my templates folder in a Flask app?


I'm trying to use render_template in my Flask app:

# -*- coding: utf-8 -*-

from flask import Flask, render_template, redirect, url_for, request

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "Hello World", 200

@app.route('/welcome')
def welcome():
    return render_template("welcome.html")

@app.route("/login", methods=["GET", "POST"])
def login():
    error = None
    if request.method == "POST":
        if request.form["username"] != "admin" or request.form["password"] != "admin":
            error = "Invalid credentials. Please try again!"
        else:
            return redirect(url_for("home"))
    return render_template("login.html", error=error)


app.run(debug=True)

I think that my app can't see welcome.html (and the templates folder) because I always get a "Internal Server Error" and no HTML page.

In a single folder, I have app.py the static folder and the templates folder (with the HTML file).

What's wrong here?


Solution

  • Does your python script have all the necessary bits of a minimal Flask application?

    from flask import Flask, render_template
    app = Flask(__name__)
    
    @app.route('/welcome')
    def welcome():
        return render_template("/welcome.html")
    
    if name== "__main__":
        app.run()