python-Web-django-后台

url:

# member 处理
    re_path('member/list/', member.list, name='member/list/'),
    re_path('member/list_part/', member.list_part, name='member/list_part/'),
    re_path('member/add/', member.add, name='member/add/'),
    re_path('member/delete/', member.delete, name='member/delete/'),
    re_path('member/change/', member.change, name='member/change/'),

views:

from django.shortcuts import HttpResponse,redirect,render
from app01.models import *
import json  # 使用json格式数据
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
import re
from back.views.ddff import defense_url,ddff_list

@defense_url
def add(request,aid=None,**kwargs):
    '''管理员添加'''
    res = {'s':None,'info':None}
    # 提交
    print(request.POST)
    if request.method == 'POST':
        # 名字
        if request.POST.get('member_name') == '':
            res['info'] = '名字不能为空'
            return HttpResponse(json.dumps(res))
        member_name = request.POST.get('member_name').strip()

        # 手机号
        if request.POST.get('member_tel') == '':
            res['info'] = '手机号不能为空'
            return HttpResponse(json.dumps(res))
        member_tel = request.POST.get('member_tel').strip()

        # 手机号格式
        if re.match('^[0-9]+$', member_tel) is None:
            res['info'] = '手机号格式不正确'
            return HttpResponse(json.dumps(res))

        # Email
        if request.POST.get('member_email') == '':
            res['info'] = 'Email不能为空'
            return HttpResponse(json.dumps(res))
        member_email = request.POST.get('member_email').strip()

        # Email格式
        if re.match('^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$', member_email) is None:
            res['info'] = 'Email格式不正确'
            return HttpResponse(json.dumps(res))

        # 昵称
        if request.POST.get('member_nickname') == '':
            res['info'] = '昵称不能为空'
            return HttpResponse(json.dumps(res))
        member_nickname = request.POST.get('member_nickname').strip()

        # 密码
        if request.POST.get('pwd') == '':
            res['info'] = '密码不能为空'
            return HttpResponse(json.dumps(res))
        pwd = request.POST.get('pwd').strip()

        # 密码格式
        if re.match('^[0-9a-zA-Z]*$', pwd) is None:
            res['info'] = '密码格式不正确'
            return HttpResponse(json.dumps(res))

        # 会员
        if request.POST.get('member_hy') != '':
            member_hy = request.POST.get('member_hy').strip()
        else:
            member_hy = 0

        # 是否已经存在了
        mm = Member.objects.filter(member_name=member_name)
        # 存在
        print(mm)
        if mm != None:
            # 修改
            mm[0].update(
                    member_name=member_name, member_pwd=pwd,member_tel=member_tel,member_email=member_email,member_nickname=member_nickname,member_hy=member_hy)
            res['s'] = 1
            res['info'] = '修改成功!'
            return HttpResponse(json.dumps(res))
        else:
            # 添加新的
            a_obj = Member(member_name=member_name, member_pwd=pwd,member_tel=member_tel,member_email=member_email,member_nickname=member_nickname,member_hy=member_hy)
            a_obj.save()
            res['s'] = 1
            res['info'] = '添加成功!'
            return HttpResponse(json.dumps(res))

    member_obj = Member.objects.filter(id=aid).first()
    return render(request,'member/add.html',locals())

@ddff_list
def list(request):
    '''Member 列表'''

    member = Member.objects.all()
    return render(request,'member/list.html',locals())


def list_part(request):
    '''ajax分页器'''
    where = getWhere(request)
    member_list = Member.objects.filter(**where).all()
    currentPage = int(request.GET.get('page', 1))
    paginator = Paginator(member_list, 5)
    if paginator.num_pages > 6:
        if currentPage - 2 < 1:
            pageRange = range(1, 6)
        elif currentPage + 2 > paginator.num_pages:
            pageRange = range(currentPage - 2, paginator.num_pages + 1)
        else:
            pageRange = range(currentPage - 2, currentPage + 2)
    else:
        pageRange = paginator.page_range
    try:
        member_list = paginator.page(currentPage)
    except PageNotAnInteger:
        member_list = paginator.page(1)
    except EmptyPage:
        member_list = paginator.page(paginator.num_pages)
    return render(request, 'member/list_part.html', locals())


def getWhere(request)->dict:
    '''where拼接'''
    where = {}
    member_id = request.POST.get('member_id')
    title = request.POST.get('title')
    member_hy = request.POST.get('member_hy')
    manage_select = request.POST.get('manage_select')
    if member_id:
        where['id'] = member_id
    if manage_select:
        where[manage_select+'__icontains'] = title
    if member_hy:
        where['member_hy'] = member_hy
    print(where)
    return where


def delete(request):
    ''''''
    if request.method == 'POST':
        id = request.POST.get('id')
    else:
        id = request.GET.get('id')
    result = Member.objects.filter(id=id).delete()
    import json
    res = {'status': None, 'info': None}
    if result:
        res['status'] = 1
        res['info'] = '操作成功'
    else:
        res['status'] = 0
        res['info'] = '操作失败'
    return HttpResponse(json.dumps(res))  # 把这个结果告诉给前台,ajax


def change(request):
    ''''''
    if request.method == 'POST':
        aid = request.POST.get('id')
    else:
        aid = request.GET.get('id')
    member = Member.objects.filter(id=aid).first()
    print(member)
    return  add(request,aid)

def alldelete(request):
    '''全部删除'''
    print(request.POST)
    res = {'status': None, 'info': None}
    # if result:
    #     res['status'] = 1
    #     res['info'] = '操作成功'
    # else:
    #     res['status'] = 0
    #     res['info'] = '操作失败'
    # return HttpResponse(json.dumps(res))  # 把这个结果告诉给前台,ajax

 

html-list:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>列表页</title>
<link href="/static/back/css/style.css" rel="stylesheet" type="text/css" />
<link href="/static/back/css/select.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.css">
<script type="text/javascript" src="/static/back/js/jquery.js"></script>
<script type="text/javascript" src="/static/back/js/jquery.idTabs.min.js"></script>
<script type="text/javascript" src="/static/back/js/select-ui.min.js"></script>
<script type="text/javascript" src="/static/layer/layer.js"></script>

 
<script type="text/javascript">
$(document).ready(function(e) {
    $(".select1").uedSelect({
        width : 345              
    });
    $(".select2").uedSelect({
        width : 167  
    });
    $(".select3").uedSelect({
        width : 100
    });
});
</script>

</head>

<body>

    <div class="place">
        <span>位置:</span>
        <ul class="placeul">
            <li><a href="{% url 'back:index/index/' %}">首页</a></li>
{#            <li><a href="{% url 'back:index/index/' %}">系统设置</a></li>#}
        </ul>
    </div>
    
    <div class="formbody">   
    <div id="usual1" class="usual">   
      <div id="tab2" class="tabson">

{#  form表单  #}
<form >
        {% csrf_token %}
    <ul class="seachform">

    <li><label>查询方式</label>
    <div class="vocation">
    <select class="select3" name="manage_select">
        <option value="">全部</option>
        <option value="member_nickname">昵称</option>
        <option value="member_tel">手机号</option>
        <option value="member_email">email</option>
    </select>
    </div>
    </li>

    <li>
        <label>关键字</label><input name="title" type="text" class="scinput" />
    </li>

    <li><label>用户</label>
    <div class="vocation">
    <select class="select3" name="member_id">
        <option value="">全部</option>
        {% for i1 in member %}
    <option value="{{ i1.id }}">{{ i1.member_name }}</option>
        {% endfor %}
    </select>
    </div>
    </li>

    <li><label>是否会员</label>
    <div class="vocation">
    <select class="select3" name="member_hy">
        <option value="">全部</option>
    <option value="1"></option>
    <option value="0"></option>
    </select>
    </div>
    </li>

    <li>
        <label>&nbsp;</label><input name="" type="button" class="scbtn" value="查询" id="search"/>
    </li>
    
    </ul>

{#  文章列表  #}
    <table class="tablelist">
        <thead>
        <tr>
        <th><input name="" type="checkbox" value="" checked="checked"/></th>
        <th>编号<i class="sort"><img src="/static/back/images/px.gif" /></i></th>
        <th>用户</th>
        <th>手机号</th>
        <th>Email</th>
        <th>昵称</th>
        <th>密码</th>
        <th>会员</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>

        <tbody class="maina">


                </tbody>


        </tbody>
    </table>
</form>

    </div>  
       
    </div>
     <script>
            //首次页面加载,通过ajax拿到数据,放在maina容器里
            show_list();
            function show_list() {
                var url = '{% url 'back:member/list_part/' %}';
                $.post(url, $('form').serialize(), function (data) {
                    $(".maina").html(data);
                });
            }
            //点击分页,点击a连接:
            $(document).on('click', '.pagin a', function () {
                $.post(this.href, $('form').serialize(), function (data) {
                    $(".maina").html(data);
                });
                return false;//让a连接失效
            });

            //点击查看
             $(document).on('click', '.change', function () {
                 id = $(this).parent().data("id");
                 location.href="/back/member/change/?id="+id;
{#                 $.post("{% url 'back:article/delete/' %}", {#}
{#                        "id": id,#}
{#                        "csrfmiddlewaretoken": "{{ csrf_token }}"#}
{#                    }, function (data) {#}
{##}
{#                 })#}
             });

            //点击删除
            $(document).on('click', '.del', function () {
               _this = this;
                layer.confirm('您确定要删除吗?', {
                    btn: ['确定', '取消']
                }, function () {
                    id = $(_this).parent().data("id");
                    $.post("{% url 'back:member/delete/' %}", {
                        "id": id,
                        "csrfmiddlewaretoken": "{{ csrf_token }}"
                    }, function (data) {
                        if (data.status == 1) {
                            layer.msg(data.info, function () {
                                $(_this).parent().parent().remove();
                            });

                        } else {
                            layer.msg(data.info);
                        }
                    }, 'json');
                }, function () {
                });
                return false;//让a连接失效
            });

            //点击全部删除
            $(document).on('click', '.alldel', function () {
               _this = this;
                layer.confirm('您确定要删除吗?', {
                    btn: ['确定', '取消']
                }, function () {
{#                    id = $(_this).parent().data("id");#}
                    $.post("{% url 'back:member/alldelete/' %}",$('form').serialize(), function (data) {
                        if (data.status == 1) {
                            layer.msg(data.info, function () {
                                $(_this).parent().parent().remove();
                            });

                        } else {
                            layer.msg(data.info);
                        }
                    }, 'json');
                }, function () {
                });
                return false;//让a连接失效
            });

            //点击查询按钮
            $(document).on('click', '#search', function () {
                show_list();
            });

        </script>

 
    <script type="text/javascript"> 
      $("#usual1 ul").idTabs(); 
    </script>
    
    <script type="text/javascript">
        $('.tablelist tbody tr:odd').addClass('odd');
    </script>
    
    </div>


</body>

</html>

html-list_part:

{% for i in member_list %}
        <tr>
        <td><input name="memberid[]" type="checkbox" value="{{ i.id }}" /></td>
        <td>{{ forloop.counter }}</td>
        <td>{{ i.member_name }}</td>
        <td>{{ i.member_tel }}</td>
        <td>{{ i.member_email }}</td>
        <td>{{ i.member_nickname }}</td>
        <td>{{ i.member_pwd }}</td>
        <td>{% if i.member_hy == '1' %}是{% else %}否{% endif %}</td>
        <td data-id="{{ i.id }}"><a href="javascript:;" class="tablelink change">修改</a> <a href="javascript:;" class="tablelink del"> 删除</a></td>
        </tr>
        {% endfor %}
<tr class="pagin"><th colspan="9">

{#           <div class="pagin">#}<span class="member" align="left"><a href="javascript:;" class="tablelink alldel">全部删除</a></span>
        <span class="member" align="right"><i class="blue">{{ paginator.count }} </i> 条 记 录 </span>
{#        <ul class="paginList">#}
{##}
{#        </ul>#}
{#    </div>#}



{#分页器#}
    <nav aria-label="Page navigation example">
  <ul class="pagination justify-content-end">
  {% if paginator.has_previous %}
    <li class="page-item disabled">
      <a class="page-link" href="/back/member/list_part/?page={{ p.previous_page_number }}" tabindex="-1" aria-disabled="true">上一页</a>
    </li>
  {% endif %}
      {% for i in pageRange %}

    <li class="page-item {% if i == currentPage %}current{% endif %}"><a class="page-link" href="/back/member/list_part/?page={{ i }}">{{ i }}<span class="sr-only">(current)</span></a></li>
          {% endfor %}

  {% if paginator.has_next %}
    <li class="page-item">
      <a class="page-link" href="/back/member/list_part/?page={{ p.next_page_number }}">下一页</a>
    </li>
  {% endif %}
  </ul>
</nav>


        </th></tr>

html-add:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="/static/back/css/style.css" rel="stylesheet" type="text/css" />
<link href="/static/back/css/select.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/static/back/js/jquery.js"></script>
<script type="text/javascript" src="/static/back/js/jquery.idTabs.min.js"></script>
<script type="text/javascript" src="/static/back/js/select-ui.min.js"></script>

<script type="text/javascript" src="/static/back/kindeditor/kindeditor-all.js"></script>
    <script>
            KindEditor.ready(function(K) {
                    window.editor = K.create('#content',{
                        width:"800",
                        height:"600",
                        resizeType:0,
                        uploadJson:"/back/article/upload/",
                        extraFileUploadParams:{
                            csrfmiddlewaretoken:"{{ csrf_token }}"
                        },
                        filePostName:"img"//上传图片的名称
                    });
            });
</script>
 
  
<script type="text/javascript">
    $(document).ready(function(e) {
        $(".select1").uedSelect({
            width : 345              
        });
        $(".select2").uedSelect({
            width : 167  
        });
        $(".select3").uedSelect({
            width : 100
        });

{#        //加载富文本编辑器#}
{#            KindEditor.ready(function(K) {#}
{#                K.create('#content', {#}
{#                    allowFileManager : true,#}
{#                    filterMode:true,#}
{#                    afterBlur:function(){#}
{#                        this.sync("#content");#}
{#                    }#}
{#                });#}
{#            });#}
    });

</script>
</head>

<body>
<form action="{% url 'back:member/add/' %}" method="post">
    {% csrf_token %}
    <div class="place">
    <span>位置:</span>
    <ul class="placeul">
    <li><a href="{% url 'back:index/index/' %}">首页</a></li>
{#    <li><a href="#">系统设置</a></li>#}
    </ul>
    </div>
    
    <div class="formbody">
    <div id="usual1" class="usual"> 
      <div id="tab1" class="tabson">
 
    
    <ul class="forminfo">

    <div class="cityright">
    </div>

        <li><label>姓名<b>*</b></label><div><input name="member_name" type='text' class="dfinput" value="{{ member_obj.member_name }}" placeholder="任意字符组合" style="width:518px;"/></div></li>
        <li><label>手机号<b>*</b></label><div><input name="member_tel" type='text' class="dfinput" value="{{ member_obj.member_tel }}" placeholder="123456789" style="width:518px;"/></div></li>
        <li><label>Email<b>*</b></label><div><input name="member_email" type='text' class="dfinput" value="{{ member_obj.member_email }}" placeholder="123@qq.com" style="width:518px;"/></div></li>
        <li><label>昵称<b>*</b></label><div><input name="member_nickname" type='text' class="dfinput" value="{{ member_obj.member_nickname }}" placeholder="任意字符组合" style="width:518px;"/></div></li>
       <li><label>密码<b>*</b></label><div><input name="pwd" type="text" class="dfinput" value="{{ member_obj.member_pwd }}" placeholder="数字和任意大小写字母组合" style="width:518px;"/></div></li>

        <div><li><label>会员<b></b></label><input name="member_hy" type="radio" {% if member_obj.member_hy == '1' %}checked="checked" {% endif %} class="" value="1"  style="width:40px;"/>
            不是<input name="member_hy" type="radio" class="" value="0" {% if member_obj.member_hy == '1' %}{% else %}checked="checked" {% endif %} style="width:40px;"/>
    </li></div>
{#        <li><label>会员<b>*</b></label><div><input name="member_hy" type='text' class="dfinput" value="{{ member_obj.member_hy }}"  style="width:518px;"/></div></li>#}
{#    <li><label>密码<b>*</b></label>#}
{#    <div class="vocation">#}
{#    <select class="select3" name="member_hy">#}
{#    <option value="0"></option>#}
{#    <option {% if member_obj.member_hy == '1' %}selected{% endif %} value="1"></option>#}
{#    </select>#}
{#    </div>#}
{#    </li>#}
    <li><label>&nbsp;</label><input name="dosubmit" type="button" class="btn" value="提交" id="submitBtn"></li>

{#    <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="dosubmit" value="提交"/></li>#}


    </ul>

    </div> 
       
    </div> 
 
    </div>
</form>

</body>

</html>
<script src="/static/js/jquery.min.js"></script>
<script src="/static/layer/layer.js"></script>
<script>
    $(document).ready(function () {
        $("#submitBtn").click( function () {
            // 点击事件
            $.post("/back/member/add/",$('form').serialize() , function (data) {
                //LoginName,Password,code
                if (data['s'] == 1) {
                    layer.msg(data['info']);
                    // 跳转主页面
                    return false
                } else {
                    //登陆失败
                    layer.msg(data['info']);
                }
                return false;
                }, "json");

        })
    })
</script>
posted @ 2019-08-21 20:03  一觉昏睡人  阅读(231)  评论(0编辑  收藏  举报