• <input id="oe200"></input>
  • <menu id="oe200"></menu>

    django模糊查詢及分頁操作實例

    時間:2022-01-28 23:11:31 類型:python
    字號:    

    views.py代碼:

    title    = request.GET.get("title","")
    sortid   = int(request.GET.get("sortid",0))
    search = {}
    search_parm = ""
    if title :
         search["title__icontains"] = title
         search_parm +="&title="+title
    if sortid :
       search_parm += "&sortid=" + str(sortid)
       sortpath = Sorts.objects.get(pk=sortid).sortpath
       search["sort__sortpath__icontains"] = sortpath
    query = News.objects.filter(**search)
    
    pageSize = int(request.GET.get("pageSize", 1))  # 默認頁碼為2
    currentPage = int(request.GET.get("currentPage", 1))  # 當前頁默認為第一頁
    
    startPos = (currentPage - 1) * pageSize  #查詢開始位置
    endPos   = currentPage * pageSize        #查詢結束位置
    result = query[startPos:endPos]          #相當于sql的limit操作
    total  = query.count()
    
    totalPages = int(total / pageSize)
    if total % pageSize:
       totalPages = total // pageSize + 1
    print("共有",totalPages)
    loop_pages = range(1,totalPages+1)
    #注意循環時不包含最大值
    
    for index, row in enumerate(result) :
        setattr(result[index],"parSorts",SortAll.getParentName(row.sort.sortpath))
    
    selectTree = SortAll.selectTree(20, "sortid", currentid=sortid, top=True )
    
    return render(request,'zz/news/list1.html',locals())

    HTML代碼:

    {% load static %}
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link rel="stylesheet" href="{% static "/css/zz.css" %}">
    </head>
    
    <body>
        <table class="list">
            <tr>
                <td>
                    <form action="?" method="get">
                        <input type="text" name="title" value="{{ title }}">
                        {{ selectTree|safe }}
                        <input type="submit" value="搜索">
                    </form>
                </td>
            </tr>
        </table>
        <table class="list">
            <tr>
                <td colspan="7">
                    <div class="page">
                        <ul>
                                {% if totalPages > 1 %}
                                 <li><a href="?currentPage=1{{ search_parm }}">首頁</a></li>
                                {% endif %}
                                {% for i in loop_pages %}
                                    <li><a href="?currentPage={{ i }}{{ search_parm }}">{{ i }}</a></li>
                                {% endfor %}
                                {% if currentPage < totalPages %}
                                    <li><a href="?currentPage={{ totalPages }}{{ search_parm }}">尾頁</a></li>
                                {% endif %}
                        </ul>
                    </div>
                </td>
            </tr>
            <tr>
                <td>id</td>
                <td>選擇</td>
                <td>標題</td>
                <td>時間</td>
                <td>類別</td>
                <td>圖片</td>
                <td>操作</td>
            </tr>
            {% for row in result %}
                <tr>
                    <td>{{ row.id }} </td>
                    <td style="width:30px;">
                        <input type="checkbox" name="ids" value="{{ row.id }}">
                    </td>
                    <td  style="max-width: 220px;">{{ row.title }}</td>
                    <td>{{ row.createtime | date:"m-d" }}</td>
                    <td>{{ row.parSorts }}</td>
                    <td>
                        {%if row.pic == "" or row.pic == null %}
                        無圖
                        {% else %}
                        <img src="{% static "up/"%}{{ row.pic }}" style="max-width: 120px;">
                        {% endif %}
    
                    </td>
                    <td>
                        <a href="/yt/news/edit?id={{ row.id }}" class="btn">修改</a>
                        <a href="/yt/news/del?id={{ row.id }}" class="btn danger" onclick="return confirm('確定要刪除嗎')">刪除</a>
                    </td>
                </tr>
                {% empty %}
                <tr>
                    <td colspan="8">
                        無數據
                    </td>
                </tr>
            {% endfor %}
        </table>
    </body>
    </html>

    效果如下:

    5.jpg

    亚洲色欲色欲在线大片
  • <input id="oe200"></input>
  • <menu id="oe200"></menu>
    <