如何使用 Jinja Flask 在列表项前面添加数字?
How to add number in front of list item with Jinja Flask?
我正在学习 flask 并创建了一个非常基本的页面,该页面呈现 pyjokes 库中的笑话
在这里我的疑问是虽然笑话在列表中填充但所有项目都有 1. 在他们面前我如何增加这些数字
Python代码
from flask import Flask, redirect, url_for, render_template
import pyjokes
import sys
app = Flask(__name__)
jokes = pyjokes.get_jokes()
# Defining the home page of our site
@app.route("/") # this sets the route to this page
def home():
# print(jokes)
return render_template("1.html" , jokes = jokes) # some basic inline html
@app.route("/<anything>")
def user(anything):
return f"Hello {anything}! plz go back nothing here "
if __name__ == "__main__":
app.run(use_reloader=True,debug=True)
HTML代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Welcome to home page</h1>
<h3>Below are jokes frok python modules jokes</h3>
{% for x in jokes %}
<ol><li>{{x}}</li></ol>
{% endfor %}
</body>
</html>
采样电流输出
Welcome to home page
Below are jokes frok python modules jokes
1.Complaining about the lack of smoking shelters, the nicotine addicted Python programmers said 1.there ought to be 'spaces for tabs'.
1.Ubuntu users are apt to get this joke.
1.Obfuscated Reality Mappers (ORMs) can be useful database tools.
& so on ...
预期输出
Welcome to home page
Below are jokes frok python modules jokes
1.Complaining about the lack of smoking shelters, the nicotine addicted Python programmers said 1.there ought to be 'spaces for tabs'.
2.Ubuntu users are apt to get this joke.
3.Obfuscated Reality Mappers (ORMs) can be useful database tools.
& so on ...
试试这个
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Welcome to home page</h1>
<h3>Below are jokes frok python modules jokes</h3>
<ol>
{% for x in jokes %}
<li>{{x}}</li>
{% endfor %}
</ol>
</body>
</html>
看起来您正在为每个元素创建一个新的有序列表,而不是一个包含所有元素的有序列表。
我正在学习 flask 并创建了一个非常基本的页面,该页面呈现 pyjokes 库中的笑话 在这里我的疑问是虽然笑话在列表中填充但所有项目都有 1. 在他们面前我如何增加这些数字
Python代码
from flask import Flask, redirect, url_for, render_template
import pyjokes
import sys
app = Flask(__name__)
jokes = pyjokes.get_jokes()
# Defining the home page of our site
@app.route("/") # this sets the route to this page
def home():
# print(jokes)
return render_template("1.html" , jokes = jokes) # some basic inline html
@app.route("/<anything>")
def user(anything):
return f"Hello {anything}! plz go back nothing here "
if __name__ == "__main__":
app.run(use_reloader=True,debug=True)
HTML代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Welcome to home page</h1>
<h3>Below are jokes frok python modules jokes</h3>
{% for x in jokes %}
<ol><li>{{x}}</li></ol>
{% endfor %}
</body>
</html>
采样电流输出
Welcome to home page
Below are jokes frok python modules jokes
1.Complaining about the lack of smoking shelters, the nicotine addicted Python programmers said 1.there ought to be 'spaces for tabs'.
1.Ubuntu users are apt to get this joke.
1.Obfuscated Reality Mappers (ORMs) can be useful database tools.
& so on ...
预期输出
Welcome to home page
Below are jokes frok python modules jokes
1.Complaining about the lack of smoking shelters, the nicotine addicted Python programmers said 1.there ought to be 'spaces for tabs'.
2.Ubuntu users are apt to get this joke.
3.Obfuscated Reality Mappers (ORMs) can be useful database tools.
& so on ...
试试这个
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Welcome to home page</h1>
<h3>Below are jokes frok python modules jokes</h3>
<ol>
{% for x in jokes %}
<li>{{x}}</li>
{% endfor %}
</ol>
</body>
</html>
看起来您正在为每个元素创建一个新的有序列表,而不是一个包含所有元素的有序列表。