Python flask实战订餐系统微信小程序-26编辑用户信息的实现

B站配套视频教程观看

编辑用户信息的实现

添加是插入数据,编辑是改变数据。

这里我们将添加和编辑当成一种动作,唯一不同的是我们要区分是添加还是编辑

如果能够拿到id的数据,就是编辑,否则是添加。

Account.py传递账户信息

@route_account.route( "/set" ,methods = ["GET","POST"])
def set():
    if request.method == "GET":
        resp_data = {}
        req = request.args
        uid = int(req.get("id", 0))
        info = None
        if uid:
            info = User.query.filter_by(uid=uid).first()
        resp_data['info'] = info
        return ops_render( "account/set.html" , resp_data)

set.html增加动态读取数据

{% extends "common/layout_main.html" %}
{% block content %}
<div class="row  border-bottom">
    <div class="col-lg-12">
        <div class="tab_title">
            <ul class="nav nav-pills">
                <li class="current">
                    <a href="{{ buildUrl('/account/index') }}">账户列表</a>
                </li>
            </ul>
        </div>
    </div>
</div>
<div class="row m-t  wrap_account_set">
	<div class="col-lg-12">
		<h2 class="text-center">账号设置</h2>
		<div class="form-horizontal m-t m-b">
			<div class="form-group">
				<label class="col-lg-2 control-label">姓名:</label>
				<div class="col-lg-10">
					<input type="text" name="nickname" class="form-control" placeholder="请输入姓名~~" value="{{info.nickname}}">
				</div>
			</div>
			<div class="hr-line-dashed"></div>
			<div class="form-group">
				<label class="col-lg-2 control-label">手机:</label>
				<div class="col-lg-10">
					<input type="text" name="mobile" class="form-control" placeholder="请输入手机~~" value="{{info.mobile}}">
				</div>
			</div>
			<div class="hr-line-dashed"></div>
			<div class="form-group">
				<label class="col-lg-2 control-label">邮箱:</label>
				<div class="col-lg-10">
					<input type="text" name="email" class="form-control" placeholder="请输入邮箱~~" value="{{info.email}}">
				</div>
			</div>
			<div class="hr-line-dashed"></div>
			<div class="form-group">
				<label class="col-lg-2 control-label">登录名:</label>
				<div class="col-lg-10">
					<input type="text" name="login_name" class="form-control" autocomplete="off" placeholder="请输入登录名~~" value="{{info.login_name}}">
				</div>
			</div>
			<div class="hr-line-dashed"></div>
			<div class="form-group">
				<label class="col-lg-2 control-label">登录密码:</label>
				<div class="col-lg-10">
					<input type="password" name="login_pwd" class="form-control" autocomplete="new-password" placeholder="请输入登录密码~~" value="******">
				</div>
			</div>
			<div class="hr-line-dashed"></div>
			<div class="form-group">
				<div class="col-lg-4 col-lg-offset-2">
                    <input type="hidden" name="id" value="{{info.uid}}">
					<button class="btn btn-w-m btn-outline btn-primary save">保存</button>
				</div>
			</div>
		</div>
	</div>
</div>
{% endblock %}
{%block js %}
<script src="{{ buildStaticUrl('/js/account/set.js') }}"></script>
{% endblock %}

set.js调整数据

                var data={
                    nickname :nickname,
                    mobile : mobile,
                    email : email,
                    login_name : login_name,
                    login_pwd : login_pwd,
                    id:$(".wrap_account_set input[name=id]").val()
                }

Account.py传递账户信息

@route_account.route( "/set" ,methods = ["GET","POST"])
def set():
    default_pwd="******"
    if request.method == "GET":
        resp_data = {}
        req = request.args
        uid = int(req.get("id", 0))
        info = None
        if uid:
            info = User.query.filter_by(uid=uid).first()
        resp_data['info'] = info
        return ops_render( "account/set.html" , resp_data)

    resp = {'code': 200, 'msg': '操作成功', 'data': {}}

    req = request.values

    id = request['id'] if 'id' in req else 0


    nickname = req['nickname'] if 'nickname' in req else ''
    mobile = req['mobile'] if 'mobile' in req else ''
    email = req['email'] if 'email' in req else ''
    login_name = req['login_name'] if 'login_name' in req else ''
    login_pwd = req['login_pwd'] if 'login_pwd' in req else ''

    if nickname is None or len(nickname)<1:
        resp['code']=-1
        resp['msg']='请输入符合规范的姓名'
        return jsonify(resp)

    if mobile is None or len(mobile)<1:
        resp['code']=-1
        resp['msg']='请输入符合规范的手机'
        return jsonify(resp)

    if email is None or len(email)<1:
        resp['code']=-1
        resp['msg']='请输入符合规范的邮箱'
        return jsonify(resp)

    if login_name is None or len(login_name)<1:
        resp['code']=-1
        resp['msg']='请输入符合规范的登录名'
        return jsonify(resp)

    if login_pwd is None or len(login_pwd)<1:
        resp['code']=-1
        resp['msg']='请输入符合规范的登录密码'
        return jsonify(resp)

    has_in = User.query.filter(User.login_name == login_name, User.uid!=id).first()
    if has_in:
        resp['code']=-1
        resp['msg']='登录名已存在,更换后再试'
        return jsonify(resp)

    user_info = User.query.filter_by(uid=id).first()
    if user_info:
        model_user = user_info
    else:
        model_user = User()
        model_user.created_time = getCurrentDate()
        model_user.login_salt = UserService.geneSalt()

    model_user.nickname = nickname
    model_user.mobile = mobile
    model_user.email = email
    model_user.login_name = login_name
    if login_pwd != default_pwd:
        model_user.login_pwd = UserService.genePwd(login_pwd, model_user.login_salt )
    model_user.update_time = getCurrentDate()

    db.session.add(model_user)
    db.session.commit()
    return jsonify(resp)
上一篇:golang操作sqlite3


下一篇:【精通Linux系列】Linux用户管理与组管理详解,让自己的操作系统自己管理