包内包含__init__.py文件
user子应用中用到的页面 html 文件全部放到 templates\user 子目录中。
随着flask程序越来越复杂,我们需要对程序进行模块化的处理,以利于大项目的开发。
from flask import Blueprint
user_bp = Blueprint('user', __name__)
@user_bp.route('/')
def user_center():
# print(url_for('user.register')) # 反向解析需要加上蓝图名称
return render_template('user/show.html', users=users)
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
from apps import create_app
app = create_app()
if __name__ == '__main__':
app.run(port=5002)
@user_bp.route('/update', methods=['GET', 'POST'], endpoint='update')
def user_update():
if request.method == 'POST':
# 获取post提交的数据
username = request.form.get('username')
return redirect('/')
if request.method == 'GET':
# 获取get提交的数据
username = request.args.get('username')
return render_template('user/update.html')
username = request.args.get('username')
username = request.form.get('username')
class User:
def __init__(self, username, password, phone=None):
self.username = username
self.password = password
self.phone = phone
def __str__(self):
return self.username
from apps.user.model import User
user = User(username, password, phone)
DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{% block title %} 用户中心 {% endblock %}title>
{% block mycss %}
{% 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>
{% block head%}{% endblock %}
div>
<div id="middle">
{% block middle %}{% endblock %}
div>
<div id="foot">
{% block foot%}{% endblock %}
div>
{% block myjs %}{% endblock %}
body>
html>
{% extends 'base.html' %}
{% block title %}
用户展示
{% endblock %}
{% block middle %}
<span>当前用户人数时:{{ users|length }} 人span>
<ul>
{% for user in users %}
<li>{{user.username}}-{{user.password}}-{{user.phone}}li>
{% endfor %}
ul>
<table border="solid 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 type="text/javascript">
function del(username){
// console.log(username)
// location 地址栏对象
location.href = '/del?username=' + username
}
function update(username){
location.href = '/update?username=' + username
}
script>
{% endblock %}
<a href="javascript:;" onclick="del('{{ user.username }}')">删除a>
<script type="text/javascript">
function del(username){
// console.log(username)
// location 地址栏对象
location.href = '/del?username=' + username
}
script>
# 配置文件
ENV = 'development'
DEBUG = True
from apps import create_app
app = create_app()
if __name__ == '__main__':
app.run(port=5002)
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
class User:
def __init__(self, username, password, phone=None):
self.username = username
self.password = password
self.phone = phone
def __str__(self):
return self.username
from flask import Blueprint, request, render_template, redirect, url_for
from apps.user.model import User
user_bp = Blueprint('user', __name__)
# 列表保存用户对象
users = []
@user_bp.route('/')
def user_center():
# print(url_for('user.register')) # 反向解析需要加上蓝图名称
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:
msg = '用户名已存在!'
print(msg)
return render_template('user/register.html', msg=msg)
#4.4蓝图1:04:35
# 创建user对象
user = User(username, password, phone)
# 添加到用户列表
users.append(user)
# print(users)
return redirect('/')
else:
return render_template('user/register.html', msg='密码不一致!')
return render_template('user/register.html')
@user_bp.route('/login', methods=['GET', 'POST'])
def login():
return '用户登录'
@user_bp.route('/logout', methods=['GET', 'POST'])
def logout():
return '用户退出'
@user_bp.route('/del')
def del_user():
# 获取传递过来的username
username = request.args.get('username')
# 根据username找到列表中的对象
for user in users:
if user.username == username:
users.remove(user)
return redirect('/')
else:
return '删除失败'
@user_bp.route('/update', methods=['GET', 'POST'], endpoint='update')
def user_update():
if request.method == 'POST':
# post请求
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 == username:
return render_template('user/update.html', msg='用户名重复!')
else:
for user in users:
if user.username == realname:
user.username = username
user.phone = phone
return redirect('/')
else:
# get请求
username = request.args.get('username')
for user in users:
# 匹配用户
if user.username == username:
return render_template('user/update.html', user=user)
DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{% block title %} 用户中心 {% endblock %}title>
<style type="text/css">
#head {
height: 3.125rem;
background-color: bisque;
}
#head ul li {
float: left;
width: 6.25rem;
text-align: center;
font-size: 1.125rem;
height: 3.125rem;
line-height: 3.125rem;
}
#middle {
height: 56.25rem;
background-color: azure;
}
#foot {
height: 3.125rem;
line-height: 3.125rem;
background-color: darkseagreen;
}
style>
{% block mycss %}
{% 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="{{ 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 %}
{% extends 'base.html' %}
{% block title %}
用户展示
{% endblock %}
{% block middle %}
<span>当前用户人数时:{{ users|length }} 人span>
<ul>
{% for user in users %}
<li>{{user.username}}-{{user.password}}-{{user.phone}}li>
{% endfor %}
ul>
<table border="solid 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 type="text/javascript">
function del(username){
// console.log(username)
// location 地址栏对象
location.href = '/del?username=' + username
}
function update(username){
location.href = '/update?username=' + username
}
script>
{% endblock %}
{% extends 'base.html' %}
{% block title %}
用户信息修改
{% endblock %}
{% block middle %}
<h1>用户信息更新h1>
<p style="color: red"> {{ msg }}p>
<form action="{{ url_for('user.update') }}" method="post">
<p><input type="hidden" name="realname" id="" value="{{ user.username }}" />p>
<p><input type="text" name="username" placeholder="用户名" value="{{ user.username }}">p>
<p><input type="text" name="password" placeholder="密码" value="{{ user.password }}" disable>p>
<p><input type="number" name="phone" placeholder="手机号码" value="{{ user.phone }}">p>
<p><input type="submit" value="用户更新">p>
form>
{% endblock %}