Blueprint简介

Blueprint它是Flask项目的一种组件式开发,可以在一个应用内或跨越 多个项目共用蓝图。使用蓝图可以极大地简化大型应用的开发难度,也为Flask扩展 提供了一种在应用中注册服务的集中式机制。 模块化管理程序路由是它的特色,它使程序结构清晰、简单易懂。但是一个Blueprint并不是一个完整的应用,它不能独立于应用运行,而必须要注册到某一个应用中。

由于只是演示蓝图的基本用法,所以这里以展示用户表,登陆认证,编辑用户,删除用户,新增用户来模拟多个模块作为演示。

环境搭建:

1、创建项目damo_2,在该项目下创建static(存放图片,css样式,js脚本等)、templates(HTML文件)两个目录。

2、新建一个settings.py的配置文件,配置文件中存放用户表

USER_INFO = [{'aaa':'111'},{'bbb':'222'},{'ccc':'333'}]  # 模拟数据库中的用户表

一、函数的方式(FBV):

1、展示用户表

1.1创建show.py

from flask import Blueprint,render_template
from settings import USER_INFO

show_app = Blueprint('show_app',__name__)
@show_app.route('/show_page')
def show_page():
    return render_template('show_page.html',user_info=USER_INFO)

 1.2在templates目录下创建show_page.html

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h3 style="margin-left: 320px">用户详情表</h3>
<form action="">
    <table border="1px">
        <thead>
        <tr>
            <th style="width: 150px;">序号</th>
            <th style="width: 150px;">用户</th>
            <th style="width: 150px;">密码</th>
            <th style="width: 150px;">操作</th>
        </tr>
        </thead>
        <tbody>
        {% for user_dict in user_info %}

            <tr>
                <td style="text-align: center">{{ loop.index0 }}</td>
                {% for user in user_dict %}
                    <td style="text-align: center">{{ user }}</td>
                    <td style="text-align: center">{{ user_dict.get(user) }}</td>
                {% endfor %}
                <td style="text-align: center">
                    <a href="/edit_user?id={{ loop.index0 }}">编辑</a>
                    |
                    <a href="/del_user?id={{ loop.index0 }}">删除</a>
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
    <br>
    <a href="/add_user">新增</a>
</form>
</body>
</html>
show_page

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-16
  • 2021-05-20
  • 2022-12-23
  • 2022-12-23
  • 2021-10-30
猜你喜欢
  • 2021-06-18
  • 2022-12-23
  • 2021-07-23
  • 2021-09-30
  • 2022-01-26
  • 2022-01-09
相关资源
相似解决方案