from flask import redirect, url_for
@app.route("/user/<name>")
def say_hi(name):
return f"Hi {name}"
@app.route("/index.html")
def index2():
return redirect(url_for('index')) # index is the name of a function
Static
Files in the static directory are served directly (without the need for any code):
Useful to place images, JavaScript, CSS, etc.
Jinja2 templates
hello.html
<!doctype html>HP
<title>Hello</title>
{% if name %}
<h1>Hello {{ name }}</h1>
{% else %}
<h1>Hello World</h1>
{% endif %}
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/user")
@app.route("/user/<name>")
def say_hi(name=None):
return render_template("hello.html", name=name)
if __name__ == "__main__":
app.run()
users.html
<!doctype html>
<title>Hello</title>
{% for user in users %}
<h1>Hello {{ user }}</h1>
{% endfor %}