会员编辑功能的实现
index.html将id字段传递过去
<a class="m-l" href="{{ buildUrl('/member/set') }}?id={{item.id}}">
<tbody>
{% if list %}
{% for item in list %}
<tr>
<td><img alt="image" class="img-circle" src="{{item.avatar}}" style="width: 40px;height: 40px;"></td>
<td>{{item.nickname}}</td>
<td>{{item.sex_desc}}</td>
<td>{{item.status_desc}}</td>
<td>
<a href="{{ buildUrl('/member/info') }}?id={{item.id}}">
<i class="fa fa-eye fa-lzg"></i>
</a>
<a class="m-l" href="{{ buildUrl('/member/set') }}?id={{item.id}}">
<i class="fa fa-edit fa-lg"></i>
Member.py添加Get处理
@route_member.route( "/set",methods=["GET","POST"] )
def set():
if request.method == "GET":
resp_data = {}
req = request.args
id = int(req.get("id", 0))
rebackUrl = UrlManager.buildUrl("member/index")
if id < 1:
return redirect(rebackUrl)
member_info = Member.query.filter_by(id=id).first()
if not member_info:
return redirect(rebackUrl)
resp_data['info'] = member_info
resp_data['current'] = 'index'
return ops_render( "member/set.html",resp_data )
member/set.html动态读取数据
<div class="form-horizontal m-t">
<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" class="form-control" placeholder="请输入会员名称" name="nickname" value="{{info.nickname}}">
</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.id}}">
<button class="btn btn-w-m btn-outline btn-primary save">保存</button>
</div>
</div>
</div>
添加set.js用于提交数据到数据库 可以参考account/set.js
;
var member_set_ops = {
init:function () {
this.eventBind();
},
eventBind:function () {
$(".wrap_member_set .save").click(function () {
var btn_target = $(this);
if(btn_target.hasClass("disabled")){
common_ops.alert("正在处理 请不要重复提交");
return;
}
var nickname_target = $(".wrap_member_set input[name=nickname]");
var nickname = nickname_target.val();
if (nickname.length < 1){
common_ops.tip("请输入规范的名称", nickname_target);
return;
}
btn_target.addClass("disabled")
var data = {
nickname:nickname,
id:$(".wrap_member_set input[name=id]").val()
}
$.ajax({
url:common_ops.buildUrl("/member/set"),
type:"POST",
data:data,
dataType:'json',
success:function (res) {
btn_target.removeClass("disabled")
var callback = null;
if(res.code == 200){
callback = function () {
window.location.href = common_ops.buildUrl("/member/index");
}
}
common_ops.alert(res.msg, callback)
}
})
});
},
};
$(document).ready(function () {
member_set_ops.init()
})
引用js
</div>
{% endblock %}
{%block js %}
<script src="{{ buildStaticUrl('/js/member/set.js') }}"></script>
{% endblock %}
python后端添加post处理
# -*- coding: utf-8 -*-
from flask import Blueprint,request,redirect,jsonify
from common.libs.Helper import ops_render,iPagination,getCurrentDate
@route_member.route( "/set",methods=["GET","POST"] )
def set():
if request.method == "GET":
resp_data = {}
req = request.args
id = int(req.get("id", 0))
rebackUrl = UrlManager.buildUrl("member/index")
if id < 1:
return redirect(rebackUrl)
member_info = Member.query.filter_by(id=id).first()
if not member_info:
return redirect(rebackUrl)
resp_data['info'] = member_info
resp_data['current'] = 'index'
return ops_render( "member/set.html",resp_data )
resp = {'code': 200, 'msg': '账户添加成功', 'data': {}}
req = request.values
id = req['id'] if 'id' in req else 0
nickname = req['nickname'] if 'nickname' in req else ''
if nickname is None or len(nickname)<1:
resp['code'] = -1
resp['msg'] = "请输入正确的用户名"
return jsonify(resp)
member_info = Member.query.filter_by(id=id).first()
if not member_info:
resp['code'] = -1
resp['msg'] = "指定会员不存在"
return jsonify(resp)
member_info.nickname = nickname
member_info.updated_time = getCurrentDate()
db.session.add(member_info)
db.session.commit()
return jsonify(resp)
成功完成账号编辑