python代码如下
from flask import Flask,render_template
from random import randint
app=Flask(__name__)
hero=["无极剑圣","蒙多","蛮族之王","曙光女神","德玛西亚之力","不祥之刃","金克斯"]
@app.route('/index')
def index():
return render_template("index.html",hero=hero)
@app.route("/choujiang")
def choujiang():
i = randint(0, len(hero) - 1)
print(randint)
return render_template("index.html",hero=hero,single_hero=hero[i])
app.run(debug=True)
html代码如下
`
<!doctype html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<H5>英雄 {{hero}}</H5></br>
<a href="./choujiang">点击抽奖</a>
<H5>抽到的英雄为: {{single_hero}}</H5></br>
</body>
</html>
这是要注意的文件路径问题,因为render_template(“”)只识取templates文件夹目录下的文件