本地部署一个flask简易项目
随机抽奖
编写app.py文件
# 让我们电脑支持服务访问
# 需要一个web框架
# pip install Flask
import random
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():
num = random.randint(0,len(hero))
return render_template('index.html',hero=hero,h=hero[num-1])
app.run(port=80,debug=True) //端口可以改,默认5000 ,debug=True作用:修改后无须多次执行
html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{hero}}<br/>
<a href="/choujiang">点我抽奖</a><br>
您抽到了:{{h}}
</body>
</html>
效果图
碰到的问题
执行报错:ImportError: cannot import name ‘soft_unicode’ from 'markupsafe’问题
原因:
由于 markupsafe 弃用警告:“soft_unicode”已重命名为“soft_str”.旧名称将在 MarkupSafe 2.1 中删除.
解决方法:安装2.1下版本
--有效
pip install markupsafe==2.0.1
tips:如需部署到服务器(端口设置0.0.0.0),即将本地文件push到远程仓库,再git拉到服务器上,或使用xftp
本文来自博客园,作者:__username,转载请注明原文链接:https://www.cnblogs.com/code3/p/16951326.html