or
block content`from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def hello_world():
name = "Flask"
players = ["Brave", "Warrior", "Wizard"]
return render_template("index.html", name_value = name, players = players)
--Create the list itself as usual --The way to pass to html is the same as other variables
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
{% for player in players: %}
<p>{{ player + "Fought monsters" }}</p>
{% endfor %}
</body>
</html>
--The for statement itself is enclosed in {% ~~%}
as in the previous if statement.
--Finally, insert {% endfor%}
and tighten
or
block content`When ʻindex.html` is referenced on the py side
index.html
{% extends "layout.html" %}
{% block content %}
<h1>aiueo</h1>
{% endblock %}
layout.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
--For the html you want to use as a template source, put {% endblock%}
immediately after {% block content%}
in <body>
--The html you want to use the template is {% extends" ~~ .html "%}
, and select the person to refer to.
--After that, delete the tags such as <head>
and <body>
, and put what you want to enter instead in {% block content%}
and {% endblock%}
.
--The image of what you typed in ʻindex was transferred to
{% block content%} in
layoutthrough the warp gate
{% block content%}`.
--For items displayed in a similar format, it will be easier if you combine them into one on the html side and change variables and paths as shown below on the py side.
python
player = "player"
@app.route("/walk")
def walk():
message = player + "Was walking in the wilderness."
return render_template("action.html", player = player, message = message)
@app.route("/attack")
def attack():
message = player + "Fought a monster."
return render_template("action.html", player = player, message = message)
html
{% extends "layout.html" %}
{% block content %}
<h1>{{ player }}Action</h1>
<p>{{ message }}</p>
{% endblock %}
――I can't think of anything like this until I get used to it
--Flask I have no time to do it, I expect it on weekends and next week
Recommended Posts