文章目录
显示
后台管理员账户相关页面搭建
一、账户登录相关页面
1.创建User对象文件
2.创建login,edit,reset-pwd方法
3.创建对应的视图层
二、账户管理相关页面
1.创建Account
2.创建index,set,info方法
3.创建对应视图层
三、实战
3.1创建login
创建user文件夹,并创建User.py文件:

文件内容如下:
# -*- coding: utf-8 -*-
from flask import Blueprint
route_user = Blueprint('user_page', __name__)
@route_user.route("/login")
def login():
return "login"
www.py中引入route_user
# -*- coding: utf-8 -*-
from application import app
from web.controllers.index import route_index
from web.controllers.user.User import route_user
app.register_blueprint( route_index,url_prefix = "/" )
app.register_blueprint( route_user,url_prefix = "/user" )
运行:

3.2渲染页面
User.py
# -*- coding: utf-8 -*-
from flask import Blueprint,render_template
route_user = Blueprint('user_page', __name__)
@route_user.route("/login")
def login():
return render_template("user/login.html")
将static和templates复制到这个文件夹下
静态资源下载地址:
链接:https://pan.baidu.com/s/1SLRWMq8dmxJWWxlO2TIP3A
提取码:xvg5

application.py添加模板的路径引用

拷贝UrlManager.py到libs文件夹下

将函数模板引入进来:

'''
将函数注入到模板中
'''
from common.libs.UrlManager import UrlManager
app.add_template_global(UrlManager.buildStaticUrl, 'buildStaticUrl')
app.add_template_global(UrlManager.buildUrl, 'buildUrl')
运行:

但是这里图片没有被正常的渲染。
原因是因为样式文件加载失败:

创建static.py文件
# -*- coding: utf-8 -*-
from flask import Blueprint,send_from_directory
from application import app
route_static = Blueprint('static', __name__)
@route_static.route("/<path:filename>")
def index( filename ):
return send_from_directory( app.root_path + "/web/static/" ,filename )

application.py添加root_path

www.py中引入route_static
# -*- coding: utf-8 -*-
from application import app
from web.controllers.index import route_index
from web.controllers.user.User import route_user
from web.controllers.static import route_static
app.register_blueprint( route_index,url_prefix = "/" )
app.register_blueprint( route_user,url_prefix = "/user" )
app.register_blueprint( route_static,url_prefix = "/static" )
运行:

可以看到成功加载文件。
3.3构建仪表盘页面
index.py渲染index.html模板

运行:

Uer.py中渲染编辑页面和重置密码页面

3.4添加账号管理页面
添加Account.py并添加url地址

www.py中引入route_account
# -*- coding: utf-8 -*-
from application import app
from web.controllers.index import route_index
from web.controllers.user.User import route_user
from web.controllers.static import route_static
from web.controllers.account.Account import route_account
app.register_blueprint( route_index,url_prefix = "/" )
app.register_blueprint( route_user,url_prefix = "/user" )
app.register_blueprint( route_static,url_prefix = "/static" )
app.register_blueprint( route_account,url_prefix = "/account" )
运行:
