赞
踩
1.做一个网页,可以进行数据的增删改查的为佳
2.用pycharm工具建flask框架
3.按照MVC标准进行ajax前后端分离的代码
4.运行实现
代码分析
因为是MVC模式,所以有个dao包,有个controller包,有个service包,然后因为有些类需要重复使用,所以有个封装类的文件,在entity包里,pycharm中Flask框架生成的app文件,动态前端页面存在templates包中,静态前端页面和css、js、img等在static包中,由于引入了其他人的页面格式,将其也放在了static包中。
app.py:(启动文件及配置的所在)
from flask import Flask, render_template,session
import config
from config_class import *
from datetime import timedelta
from controller.usercontroller import usercontroller
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'AAAGGGGCCFDDDFFFDDDD'
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=30) #设置session的保存时间。
# 是使用config py文件定义配置
app.config.from_object(config)
# 是使用class定义配置
app.config.from_object(DebugConfig)
app.register_blueprint(usercontroller) # 声明
@app.route('/') # URL路由
def index():
print(session.get('user'))
return render_template('index.html')
if __name__ == '__main__':
app.run(host=app.config['SERVER_ADDR'], port=app.config['SERVER_PORT'], debug=True)
config.py:(可以在app.py中做配置,就不需要这个文件了)
'''
自定义配置文件
'''
DEBUG = True
SECRET_KEY = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
PAGE_SIZE = 10
SERVER_ADDR = '10.0.14.204'
SERVER_PORT = 80
usercontroller.py(创建.do事件,控制模型与页面视图一一对应)
from flask import Blueprint,render_template,request,session,Response
import hashlib
from service.userservice import UserService
from entity.user import User
import json
usercontroller = Blueprint('usercontroller', __name__)
userService = UserService()
@usercontroller.route("/gologin.do", methods=['POST', 'GET'])
def gologin():
return render_template('login.html')
@usercontroller.route("/login.do", methods=['POST', 'GET'])
def login():
userName = request.form.get('username') # 表单提交的数据使用request.form[]
userPwd = request.form.get('userpwd')
print("login.do:",userName)
result = userService.findUserByUserName(userName)
print("login.do:",result)
if result and result[0][2] == userPwd:
session['user'] = userName
print(hashlib.md5('123456'.encode(encoding='utf-8')).hexdigest())
# 登录成功,则保存Cookies信息
resp = Response(render_template('index.html', messasge='登录成功'))
resp.set_cookie('userName', userName, max_age=7*24*3600)
resp.set_cookie('userPwd', userPwd, max_age=7*24*3600)
return resp
pass
elif not result or result[0][2] != userPwd:
return render_template('login.html', message='用户名或密码错误')
pass
return render_template('login.html')
pass
@usercontroller.route('/logout.do', methods=['POST', 'GET'])
def logout():
session.clear()
print('==========', session.get('user'))
resp = Response(render_template('index.html'))
resp.delete_cookie('userName')
resp.delete_cookie('userPwd')
return resp
pass
@usercontroller.route('/goregist.do')
def goRegist():
return render_template('regist.html')
pass
@usercontroller.route('/regist.do', methods=['POST', 'GET'])
def regist():
userName = request.form.get('userName') # 表单提交的数据使用request.form[]
userPwd = request.form.get('userPwd')
user = User()
user.userName = userName
user.userPwd = userPwd
result = userService.createUser(user)
if result > 0:
return render_template('login.html', message='注册成功')
pass
else:
return render_template('regist.html', message='注册失败')
pass
pass
# 查询用户,并且显示
@usercontroller.route('/gouserinfo.do', methods=['POST', 'GET'])
def goUserInfo():
userName = request.form.get('userName')
userSex = request.form.get('userSex')
user = User()
user.userName = userName
user.userSex = userSex
userList = userService.findUserList(user)
return render_template('systeminfo/userinfo.html', userList = userList, user=user)
pass
# 删除用户,并且显示
@usercontroller.route('/removeruser.do', methods=['POST', 'GET'])
def goRemoveUser():
userId = request.args.get('userId')
userName = request.form.get('userName')
userSex = request.form.get('userSex')
user = User()
user.userName = userName
user.userSex = userSex
result = userService.removeUser(userId)
userList = userService.findUserList(user)
return render_template('systeminfo/userinfo.html', userList = userList, message='删除成功' if result >0 else '删除失败')
pass
@usercontroller.route('/gomodifyuser.do', methods=['POST', 'GET'])
def goModifyUser():
userId = request.args.get('userId')
user = userService.findUserByUserId(userId)
return render_template('systeminfo/modifyuser.html', user=user)
pass
@usercontroller.route('/gomodifysubmit.do', methods=['POST', 'GET'])
def goSubmitModifyUser():
userId = request.form.get('userId')
userName = request.form.get('userName')
userSex = request.form.get('userSex')
user = User()
user.userName = userName
user.userSex = userSex
user.userId = userId
result = userService.updateUser(user)
userList = userService.findUserList(user)
return render_template('systeminfo/userinfo.html', userList=userList, message='修改成功' if result > 0 else '修改失败')
pass
@usercontroller.route('/checkusername.do', methods=['POST', 'GET'])
def checkUserName():
userData = request.get_data()
userDict = json.loads(userData)
user = userService.findUserByUserName(userDict.get('userName'))
dicts = {}
if user:
dicts = {'code':1}
pass
else:
dicts = {'code':0}
pass
return json.dumps(dicts)
pass
@usercontroller.route('/goajaxuserinfo.do', methods=['POST', 'GET'])
def goAjaxUserInfo():
return render_template("systeminfo/userinfoajax.html")
pass
@usercontroller.route(
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。