flask框架web网页简单运用

<!--index2.html -->
<!DOCTYPE html>
<html lang="en">
<head>    <meta charset="UTF-8">
    <title>第一个网页标题</title>
</head>123<br/>
{{jiangchi}}  <br/> <!--这里获取的是return render_template('index2.html',jiangchi=jiangchi)-->
<a href="/choujiang">随机抽取</a>   <br/> <!--调取choujiang函数接口-->
您抽到了:{{h}}  <!-- choujiang接口返回体中随机数h-->
</body>
</html>
##appweb.py
#通过浏览器访问我们的代码
from flask import Flask,render_template
from random import randint
#抽奖
jiangchi=['电视','冰箱','电脑','书籍红楼梦','电饭煲','书桌']
app=Flask(__name__)
@app.route('/index')
def index():
    #return "hello,访问浏览器网页" #浏览器页面中直接展示
     return render_template('index2.html',jiangchi=jiangchi)#返回一个模板文件:index2.html
@app.route('/choujiang')
def choujiang ():
    num= randint(0,len(jiangchi)-1)#随机生成一个数字
    return render_template('index2.html',jiangchi=jiangchi,h=jiangchi[num])
app.run(debug=True)#本地服务器一直运行,不关闭,方便调试

访问地址:http://127.0.0.1:5000/choujiang
HTML运行页面如下:
123
[‘电视’, ‘冰箱’, ‘电脑’, ‘书籍红楼梦’, ‘电饭煲’, ‘书桌’]
随机抽取
您抽到了:电脑

HTML页面可以优化一下:文字居中加粗

<!--index.html -->
<!DOCTYPE html>
<html>
    <head> <meta charset="utf-8">
        <style>
            .bold-text{
                font-weight: bold;
                text-align: center;
            }
        </style>
        <title>第一个页面</title>
    </head>
    <body>
       <p class="bold-text"> 123大家来抽奖了<br/></p><!-- .bold-text这里 bold-text前面的点.要去掉,不然样式不生效-->
       <p class="bold-text"> {{jiangchi}}<br/></p>
        <p class="bold-text"> <a href="/choujiangAPI">随机抽奖</a></p><br/>
        <p class="bold-text">  你抽到里:{{H}}</p>
    </body>
</html>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值