Last time: Continuation of Web application development using Flask.
I will add a new routing to ʻapp.py` created last time.
app.py
from flask import Flask, render_template
app = Flask(__name__)
#Abbreviation
@app.route("/hello/<whom>")
def hello(whom):
return render_template("hello.html", whom=whom)
<whom>
is a variable, for example, if you access with / hello / taro
, taro will be included in whom.
With return render_template ("hello.html", name = whom)
, you can use it without hello.html
by assigning the taro
received from the routing to the variable name
. I can do it.
Flask searches the templates folder for templates, so you need to create the directory templetes
.
$ midir templates
Create hello.html in templetes
.
hello.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<h1>Using Jinja2 Template engine</h1>
<h2>Hello {{name}}</h2>
</body>
</html>
Web application development "first step" with VS Code and Flask
Recommended Posts