Flask Bootstrap #3 - BootStrap快速入门

Reference

  • https://www.bilibili.com/video/BV1AQe9eqEj7/?p=2&spm_id_from=pageDriver&vd_source=3d4b12fb4a4bfbc98942d43612ae2fb9

1 BootStrap

BootStrap,别人写好的一堆css样式,我们可以直接拿过来用,我们只要掌握两点就可以开始使用BootStrap:

  • BootStrap中什么样式对应什么效果
  • 如何在项目中引入BootStrap

2 下载BootStrap

进入下载地址,下载生产文件,将文件夹解压后,把文件夹放到 static 文件夹下
在这里插入图片描述

3 使用BootStrap

3.1 测试

templates 文件夹下新建一个 test.html,添加一个button标签,如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>bootstrap_test</title>
</head>
<body>
    <input type="button" value="提交">
</body>
</html>

用浏览器打开,可以看到此时没有css样式体现
在这里插入图片描述

3.2 使用BootStrap

  1. 使用 link 引入BootStrap
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>bootstrap_test</title>
    <link rel="stylesheet" href="../static/bootstrap-4.6.2-dist/css/bootstrap.min.css">
</head>
<body>
    <input type="button" value="提交">
</body>
</html>
  1. 进入bootstrap官方文档,查询button的样式
    在这里插入图片描述
  2. 对应修改 input 标签的class则会得到对应的样式效果
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>bootstrap_test</title>
    <link rel="stylesheet" href="../static/bootstrap-4.6.2-dist/css/bootstrap.min.css">
</head>
<body>
    <input type="button" class="btn btn-primary" value="提交">
</body>
</html>

在这里插入图片描述

4 在flask中启动对应html

修改 app 文件夹中的 __init__.pyroute.py

  • __init__.py

    from flask import Flask
    from app.route import hello_world,test_html
    	
    def create_app():
        app = Flask(__name__,template_folder='../templates',static_folder="../static")
        app.add_url_rule('/hello_world', 'hello_world', hello_world)
        app.add_url_rule('/test_html', 'test_html', test_html)
        return app
    
  • route.py

    from flask import render_template
    
    def hello_world():
        return "Hello, MVC框架!"
    
    def test_html():
        return render_template("test.html")
    
  • 修改 test.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>bootstrap_test</title>
        <link rel="stylesheet" href="{{url_for('static',filename='bootstrap-4.6.2-dist/css/bootstrap.min.css')}}">
    </head>
    <body>
        <input type="button" class="btn btn-primary" value="提交">
    </body>
    </html>
    

修改相对路径相关的设置是为了能让 jinjia2 能够正确找到文件,运行 python main.py ,手动跳转到/test_html,网页运行正常
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值