zoukankan      html  css  js  c++  java
  • python-Web-django-ajax分页

    views:

    from django.shortcuts import HttpResponse,redirect,render
    from app01.models import *
    import json  # 使用json格式数据
    from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
    from back.views.ddff import defense_url,ddff_list
    
    @defense_url
    def add(request,aid=None):
        '''管理员添加'''
        res = {'s':None,'info':None}
    
        # 提交
        print(request.POST)
        if request.method == 'POST':
            # 标题
            if request.POST.get('article_title') == '':
                res['s']= 1
                res['info'] = '标题不能为空'
                return HttpResponse(json.dumps(res))
            article_title = request.POST.get('article_title').strip()
    
            # 标题查询
            c_article_obj = Article.objects.filter(article_title=article_title).first()
            print(c_article_obj)
            # 找不到
            if c_article_obj is None:
                res['info'] = '标题查询不到'
                res['title'] = str(Article.objects.filter(article_title__icontains=article_title).first())
                print(res['title'])
                res['t'] = '1'
                if res['title'] is None:
                    res['t'] = '0'
                return HttpResponse(json.dumps(res))
            # 用户id
            # if request.POST.get('memberid') == '':
            #     res['info'] = '用户不能为空'
            #     return HttpResponse(json.dumps(res))
            member_id = request.POST.get('memberid')[0].strip()
    
            # 时间
            if request.POST.get('time') == '':
                res['info'] = '时间不能为空'
                return HttpResponse(json.dumps(res))
            time1 = request.POST.get('time').strip()
    
            # 内容
            if request.POST.get('content') == '':
                res['s'] = 1
                res['info'] = '内容不能为空'
                return HttpResponse(json.dumps(res))
            content = request.POST.get('content').strip()
    
            # 是否评论修改
            comment_id = request.POST.get('commentid').strip()
            if comment_id == '':
                comment_id = 0
            # 评论id是否存在
            mm = Comment.objects.filter(id=comment_id)
    
            if mm == '':
                # 修改
                print(mm)
                assert print(1)
                mm[0].update(
                comment_content=content,comment_addtime=time1,article_id=c_article_obj,member_id=member_id)
                res['s'] = 1
                res['info'] = '用户修改成功!'
                return HttpResponse(json.dumps(res))
            else:
                # 新建品论
                a_obj = Comment(comment_content=content,comment_addtime=time1,article=c_article_obj,member_id=member_id)
                a_obj.save()
                res['s'] = 1
                res['info'] = '添加成功!'
                return HttpResponse(json.dumps(res))
    
        comment_obj = Comment.objects.filter(id=aid).first()
        members = Member.objects.all()
        articles = Article.objects.all()
        return render(request,'comment/add.html',locals())
    
    @ddff_list
    def list(request):
        '''manage 列表'''
    
        comment = Comment.objects.all()
        members = Member.objects.all()
        return render(request,'comment/list.html',locals())
    
    
    def list_part(request):
        '''ajax分页器'''
        where = getWhere(request)
        comment_list = Comment.objects.filter(**where).all()
        currentPage = int(request.GET.get('page', 1))
        paginator = Paginator(comment_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:
            comment_list = paginator.page(currentPage)
        except PageNotAnInteger:
            comment_list = paginator.page(1)
        except EmptyPage:
            comment_list = paginator.page(paginator.num_pages)
        return render(request, 'comment/list_part.html', locals())
    
    
    def getWhere(request)->dict:
        '''where拼接'''
        where = {}
        title = request.POST.get('title')
        aid = request.POST.get('aid')
        article_clicknum = request.POST.get('article_clicknum')
        member_id = request.POST.get('member_id')
        if title:
            where['article__article_title__icontains'] = title
        if article_clicknum:
            if article_clicknum == '3':
                where['article__article_clicknum__gt'] = 1000
            elif article_clicknum == '2':
                where['article__article_clicknum__gt'] = 500
            elif article_clicknum == '1':
                where['article__article_clicknum__gt'] = 0
        if member_id:
            where['member_id'] = member_id
        if aid:
            where['id'] = aid
        print(where)
        return where
    
    
    def delete(request):
        ''''''
        if request.method == 'POST':
            id = request.POST.get('id')
        else:
            id = request.GET.get('id')
        result = Comment.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')
        comment = Comment.objects.filter(id=aid).first()
        print(comment)
        return  add(request,aid)
    
    
    @defense_url
    def comment(request):
        '''品论'''
        if request.method == 'POST':
            aid = request.POST.get('id')
        else:
            aid = request.GET.get('id')
        comments = Comment.objects.filter(article_id=aid).all()
        article_obj = Article.objects.filter(id=aid).first()
        print('评论')
        return render(request, 'comment/list.html', locals())

    html:

    <!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 %}
    {% if aid %}
        <input type="hidden" name="aid" value="{{ aid }}">
    {% else %}
        <ul class="seachform">
    
        <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 members %}
        <option value="{{ i1.id }}">{{ i1.member_name }}</option>
            {% endfor %}
        </select>
        </div>
        </li>
    
    
    {#    <li><label>点击量</label>#}
    {#    <div class="vocation">#}
    {#    <select class="select3" name="article_clicknum">#}
    {#        <option value="">全部</option>#}
    {#    <option value="3">热度</option>#}
    {#    <option value="2">普通</option>#}
    {#    <option value="1">少量</option>#}
    {#    </select>#}
    {#    </div>#}
    {#    </li>#}
    
        <li>
            <label>&nbsp;</label><input name="" type="button" class="scbtn" value="查询" id="search"/>
        </li>
    
        </ul>
    {% endif %}
    </form>
            <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>文章</th>
            <th>用户</th>
            <th>操作</th>
            </tr>
            </thead>
            <tbody>
    
            <tbody class="maina">
    
    
                    </tbody>
    
    
            </tbody>
        </table>
    
    
        </div>
    
        </div>
         <script>
    
                //首次页面加载,通过ajax拿到数据,放在maina容器里
                show_list();
                function show_list() {
                    var url = '{% url 'back:comment/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 () {
                    var id = $(this).parent().data("id");
                     location.href="/back/comment/change/?id="+id;
    {#                 $.post("{% url 'back:article/delete/' %}", {#}
    {#                        "id": id,#}
    {#                        "csrfmiddlewaretoken": "{{ csrf_token }}"#}
    {#                    }, function (data) {#}
    {##}
    {#                 })#}
                 });
    
                 //点击品论
                 $(document).on('click', '.comment', function () {
                     var id = $(this).parent().data("id");
                     location.href="/back/comment/comment/?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:comment/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', '#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 comment_list %}
            <tr>
            <td><input name="" type="checkbox" value="" /></td>
            <td>{{ forloop.counter }}</td>
            <td>{{ i.comment_content }}</td>
            <td>{{ i.comment_addtime }}</td>
            <td>{{ i.article }}</td>
            <td>{{ i.member }}</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="7">
    
    {#           <div class="pagin">#}
            <span class="message " 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/comment/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/comment/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/comment/list_part/?page={{ p.next_page_number }}">下一页</a>
        </li>
      {% endif %}
      </ul>
    </nav>
            </th></tr>
  • 相关阅读:
    HTML5 & CSS3编程入门经典 ((美)Rob Larsen) pdf扫描版
    HTML5+JavaScript动画基础 完整版 中文pdf扫描版
    HTML5程序开发范例宝典 完整版 (韩旭等著) 中文pdf扫描版
    HTML5从入门到精通(明日科技) 中文pdf扫描版
    HTML5秘籍(第2版) 中文pdf扫描版
    HTML5与CSS3实例教程(第2版) 附源码 中文pdf扫描版
    windows下一分钟配置ngnix实现HLS m3u8点播
    linux下搭建生成HLS所需的.ts和.m3u8文件
    使用Flash Media Server(FMS)录制mp4格式的视频
    FMS 客户端带宽计算、带宽限制
  • 原文地址:https://www.cnblogs.com/person1-0-1/p/11367606.html
Copyright © 2011-2022 走看看