#需要web框架
#pip install Flask
from flask import Flask,render_template
from random import randint
from gevent import pywsgi
app = Flask(__name__)#创建一个应用
hero = ['黑暗', '猪八戒',' 沙和尚', '蜘蛛侠', '奥特曼']
@app.route('/index')
def index():
return render_template('index.html', hero = hero)
@app.route('/choujiang')
def choujiang():
num = randint(0, len(hero)-1 )
return render_template('index.html', hero = hero, h = hero[num])
server = pywsgi.WSGIServer(('0.0.0.0', 5000), app)
server.serve_forever()
html文件内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
{{hero}}<br/>
<a href="/choujiang">随机抽取</a><br/>
您抽到了:{{ h }}<br/>
</body>
</html>
出现如下报错:
PS D:\LearningPro\Practice\python> def index():
所在位置 行:1 字符: 11
+ def index():
+ ~
“(”后面应为表达式。
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedExpression