赞
踩
目录
4、修改apps下__init__创建一个flask得app函数
2、user下新建resgister.html show.html login.html
1、templates/user下新增update.html
Flask 使用了 蓝图 的概念在一个应用或者跨应用中构建应用组件以及支持通用模式。 蓝图很好地简化了大型应用工作的方式,并提供给 Flask 扩展在应用上注册操作的核心方法。 一个 Blueprint
对象与 Flask
应用对象的工作方式很像,但它确实不是一个应用, 而是一个描述如何构建或扩展应用的 蓝图 。
Flask中的蓝图旨在针对这些情况:
Flask 中的蓝图不是即插应用,因为它实际上并不是一个应用 – 它是可以注册,甚至可以多次注册到应用上的操作集合。为什么不使用多个应用对象?你可以做到那样 (见 应用调度 ),但是你的应用会有分开的配置,并在 WSGI 层管理。
蓝图作为 Flask 层提供分割的替代,共享应用配置,并且可以更改所注册的应用对象。其短板是你不能在应用创建后撤销注册一个蓝图而不销毁整个应用对象。
蓝图的基本设想是它们记录注册到一个应用时的操作执行情况。 当从一个端点到另一端分发请求和生成 URL 时,Flask 关联视图函数和蓝图。
- from flask import Blueprint
-
- user_bp = Blueprint('user', __name__)
-
-
- @user_bp.route('/')
- def user_center():
- return '用户中心'
-
-
- @user_bp.route('/register', methods=['GET', 'POST'])
- def register():
- return '用户注册'
-
-
- @user_bp.route('/login', methods=['GET', 'POST'])
- def login():
- return '用户登录'
-
-
- @user_bp.route('/logout', methods=['GET', 'POST'])
- def logout():
- return '用户退出'
- from flask import Flask
- import settings
- from apps.user.view import user_bp
-
-
- def create_app():
- app = Flask(__name__)
- # 加载配置
- app.config.from_object(settings)
- # 蓝图 ,将蓝图对象绑定到app上
- app.register_blueprint(user_bp)
- print(app.url_map)
- return app
-
-
- if __name__ == '__main__':
- app = create_app()
- app.run()
- from flask import Flask
-
- from apps import create_app
-
- app = create_app()
-
- if __name__ == '__main__':
- app.run()
一个简单示例完成
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>
- {% block title %} 父模板的title{% endblock %}
- </title>
- <style>
- #head{
- height: 50px;
- background-color: bisque;
- }
-
- #head ul{
- list-style: none;
- height: 50px;
- }
- #head ul li{
- float: left;
- width: 100px;
- text-align: center;
- font-size: 15px;
- line-height: 50px;
- }
-
- #middle{
- height: 100%;
- background-color: azure;
- }
- #foot{
- height: 50px;
- line-height: 50px;
- background-color: darkolivegreen;
- }
- </style>
- {% block modle_style %}{% endblock %}
-
- </head>
- <body>
- <div id="head">
- <ul>
- <li><a href="">首页</a></li>
- <li><a href="">秒杀</a><</li>
- <li><a href="">超市</a><</li>
- <li><a href="">图书</a><</li>
- <li><a href="">会员</a><</li>
-
- </ul>
- </div>
-
- <div id="middle">
- {% block middle %} 中间{% endblock %}
- </div>
- <div id="foot">
- 我是底部
- </div>
- {% block myjs %}{% endblock %}
- </body>
- </html>
- {% extends 'base.html' %}
- {% block title %}
- 用户注册
- {% endblock %}
- {% block middle %}
- <p style="color: red">{{ msg }}</p>
- <!--<form action="/register" method="post"> -->
- <form action="{{url_for('user.register')}}" method="post">
- <p><input type="text" name="username" placeholder="用户名"></p>
- <p><input type="password" name="password" placeholder="密码"></p>
- <p><input type="password" name="repassword" placeholder="确认密码"></p>
- <p><input type="number" name="phone" placeholder="手机号码"></p>
- <p><input type="submit" value="用户注册"></p>
- </form>
- {% endblock %}
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # @Time : 2021/12/14 14:25
- # @Author : niubobo
- # @File : view.py
- # @Software: PyCharm
- from flask import Blueprint, request, render_template, redirect
-
- from apps.user.model import User
-
- user_bp = Blueprint('user', __name__)
- # 初始化一个列表保存注册用户信息
- users = []
-
-
- @user_bp.route('/')
- def user_center():
- return render_template('user/show.html', users=users)
-
-
- @user_bp.route('/register', methods=['GET', 'POST'])
- def register():
- if request.method == 'POST':
- # 获取post提交得数据
- username = request.form.get('username')
- password = request.form.get('password')
- repassword = request.form.get('repassword')
- phone = request.form.get('phone')
- if password == repassword:
- # 用户唯一
- for user in users:
- if user.username == username:
- return render_template('user/register.html', msg = '用户名已存在')
- # 创建用户对象
- user = User(username, password, phone)
- # 添加到用户列表
- users.append(user)
- return redirect('/')
- return render_template('user/register.html')
- # return '用户注册'
-
-
- @user_bp.route('/login', methods=['GET', 'POST'])
- def login():
- return '用户登录'
-
-
- @user_bp.route('/logout', methods=['GET', 'POST'])
- def logout():
- return '用户退出'
- {% extends 'base.html' %}
- {% block title %}
- 用户信息
- {% endblock %}
- {% block middle %}
- <h1>用户信息</h1>
- <span>当前用户人数:{{ users|length }}人</span>
- <table border="1" cellspacing="0" width="60%">
- {% for user in users %}
- <tr>
- <td>{{ loop.index }}</td>
- <td>{{ user.username }}</td>
- <td>{{ user.password }}</td>
- <td>{{ user.phone }}</td>
- <td><a href="">修改</a> <a href="">删除</a></td>
- </tr>
- {% endfor %}
-
- </table>
- {% endblock %}
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # @Time : 2021/12/11 13:52
- # @Author : niubobo
- # @File : __init__.py.py
- # @Software: PyCharm
- from flask import Flask
- import settings
- from apps.user.view import user_bp
-
-
- def create_app():
- app = Flask(__name__, template_folder='../templates', static_folder='../static')
- # 加载配置
- app.config.from_object(settings)
- # 蓝图 ,将蓝图对象绑定到app上
- app.register_blueprint(user_bp)
- print(app.url_map)
- return app
-
-
- if __name__ == '__main__':
- app = create_app()
- app.run()
输入正确内容点击【用户注册】
验证用户名重复
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # @Time : 2021/12/14 14:25
- # @Author : niubobo
- # @File : view.py
- # @Software: PyCharm
- from flask import Blueprint, request, render_template, redirect
-
- from apps.user.model import User
-
- user_bp = Blueprint('user', __name__)
- # 初始化一个列表保存注册用户信息
- users = []
-
-
- @user_bp.route('/')
- def user_center():
- return render_template('user/show.html', users=users)
-
-
- @user_bp.route('/register', methods=['GET', 'POST'])
- def register():
- if request.method == 'POST':
- # 获取post提交得数据
- username = request.form.get('username')
- password = request.form.get('password')
- repassword = request.form.get('repassword')
- phone = request.form.get('phone')
- if password == repassword:
- # 用户唯一
- for user in users:
- if user.username == username:
- return render_template('user/register.html', msg='用户名已存在')
- # 创建用户对象
- user = User(username, password, phone)
- # 添加到用户列表
- users.append(user)
- return redirect('/')
- return render_template('user/register.html')
- # return '用户注册'
-
-
- @user_bp.route('/login', methods=['GET', 'POST'])
- def login():
- return '用户登录'
-
-
- @user_bp.route('/del')
- def del_user():
- # 获取传递得username
- username = request.args.get('username')
- print(username)
- print(users)
- # 根据username找到列表users当中得user对象
- for user in users:
- if user.username == username:
- # 删除用户
- users.remove(user)
- return redirect('/')
- else:
- return '删除失败'
-
-
- @user_bp.route('/logout', methods=['GET', 'POST'])
- def logout():
- return '用户退出'
-
-
- {% extends 'base.html' %}
- {% block title %}
- 用户信息
- {% endblock %}
- {% block middle %}
- <h1>用户信息</h1>
- <span>当前用户人数:{{ users|length }}人</span>
- <table border="1" cellspacing="0" width="60%">
- {% for user in users %}
- <tr>
- <td>{{ loop.index }}</td>
- <td>{{ user.username }}</td>
- <td>{{ user.password }}</td>
- <td>{{ user.phone }}</td>
- <td><a href="">修改</a> <a href="javascript:;" onclick="del('{{ user.username }}')">删除</a></td>
- </tr>
- {% endfor %}
-
- </table>
- {% endblock %}
- {% block myjs %}
- <script>
- function del(username) {
- // console.log(username)
- // location 地址栏对象
- location.href = '/del?username='+username
- }
- </script>
- {% endblock %}
- {% extends 'base.html' %}
- {% block title %}用户更新{% endblock %}
- {% block middle %}
- <form action="{{ url_for('user.update_user') }}" method="post">
- <p><input type="hidden" name="realname" value="{{ user.username }}"></p>
- <p><input type="text" name="username" placeholder="用户名" value="{{ user.username }}"></p>
- <p><input type="password" name="password" placeholder="密码" value="{{ user.password }}" disabled></p>
- <p><input type="number" name="phone" placeholder="手机号码" value="{{ user.phone }}"></p>
- <p><input type="submit" value="用户更新"></p>
- </form>
- {% endblock %}
- {% extends 'base.html' %}
- {% block title %}
- 用户信息
- {% endblock %}
- {% block middle %}
- <h1>用户信息</h1>
- <span>当前用户人数:{{ users|length }}人</span>
- <table border="1" cellspacing="0" width="60%">
- {% for user in users %}
- <tr>
- <td>{{ loop.index }}</td>
- <td>{{ user.username }}</td>
- <td>{{ user.password }}</td>
- <td>{{ user.phone }}</td>
- <td><a href="javascript:;" onclick="update('{{ user.username }}')">修改</a>
- <a href="javascript:;" onclick="del('{{ user.username }}')">删除</a></td>
- </tr>
- {% endfor %}
-
- </table>
- {% endblock %}
- {% block myjs %}
- <script>
- //删除函数
- function del(username) {
- // console.log(username)
- // location 地址栏对象
- location.href = '/del?username='+username
- }
- //修改函数
- function update(username) {
- location.href = '/update?username='+username
- }
- </script>
- {% endblock %}
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # @Time : 2021/12/14 14:25
- # @Author : niubobo
- # @File : view.py
- # @Software: PyCharm
- from flask import Blueprint, request, render_template, redirect
-
- from apps.user.model import User
-
- user_bp = Blueprint('user', __name__)
- # 初始化一个列表保存注册用户信息
- users = []
-
-
- @user_bp.route('/')
- def user_center():
- return render_template('user/show.html', users=users)
-
-
- @user_bp.route('/register', methods=['GET', 'POST'])
- def register():
- if request.method == 'POST':
- # 获取post提交得数据
- username = request.form.get('username')
- password = request.form.get('password')
- repassword = request.form.get('repassword')
- phone = request.form.get('phone')
- if password == repassword:
- # 用户唯一
- for user in users:
- if user.username == username:
- return render_template('user/register.html', msg='用户名已存在')
- # 创建用户对象
- user = User(username, password, phone)
- # 添加到用户列表
- users.append(user)
- return redirect('/')
- return render_template('user/register.html')
- # return '用户注册'
-
-
- @user_bp.route('/login', methods=['GET', 'POST'])
- def login():
- return '用户登录'
-
-
- @user_bp.route('/del')
- def del_user():
- # 获取传递得username
- username = request.args.get('username')
- print(username)
- print(users)
- # 根据username找到列表users当中得user对象
- for user in users:
- if user.username == username:
- # 删除用户
- users.remove(user)
- return redirect('/')
- else:
- return '删除失败'
-
-
- @user_bp.route('/update', methods=['GET', 'POST'])
- def update_user():
- if request.method == 'POST':
- # 这个realname目的是做主键,防止界面username对象被改了,你找不到对象做操作
- realname = request.form.get('realname')
- username = request.form.get('username')
- password = request.form.get('password')
- phone = request.form.get('phone')
- for user in users:
- if user.username == realname:
- user.username = username
- user.phone = phone
- # return '更改成功'
- return redirect('/')
-
-
- else:
- username = request.args.get('username')
- for user in users:
- if user.username == username:
- return render_template('user/update.html', user=user)
-
-
-
-
- @user_bp.route('/logout', methods=['GET', 'POST'])
- def logout():
- return '用户退出'
-
-
注册
修改
查看修改结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。