欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Django 簡單實現(xiàn)分頁與搜索功能的示例代碼

 更新時間:2019年11月07日 11:56:30   作者:s_kangkang_A  
這篇文章主要介紹了Django 簡單實現(xiàn)分頁與搜索功能的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

假設(shè)現(xiàn)有需求如下:

需要一個頁面分頁展示信息,在該頁面添加搜索框以提供檢索功能。

那么,我們知道,展示信息和檢索功能是在同一個頁面,也就是共用一個路由。

代碼如下:

第一步,寫路由:為了清晰,這里只給出主頁和展示頁面的路由。

urls.py:

from django.urls import path
from . import views
from django.conf.urls.static import static
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
 
# from django.contrib import staticfiles
 
urlpatterns = [
 # 主頁
 path('', views.index),
 
 # 訪問他人信息入口,分頁展示
 path('other/profile', views.request_user),
 
 
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()

第二步,寫視圖:這里做了訪問限制,登錄才能訪問

views.py:

from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
from django.http import HttpResponseRedirect, HttpResponse
from django.contrib.auth import authenticate, login, logout
from .models import UserInfo, Email_Message, Wastes
from django.contrib.auth.models import AnonymousUser
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
 
@login_required(login_url='/tologin')
def request_user(request):
 # 實現(xiàn)搜索
 key = request.GET.get('key')
 all_users = UserInfo.objects.filter(cate="買家").all()
 userlist = []
 # 如果前端傳入關(guān)鍵字,才會進行檢索,否則顯示全部買家
 if key:
  for user in all_users:
   if key in user.address:
    userlist.append(user)
  all_users = userlist
 if all_users:
  paginator = Paginator(all_users, 2)
  page = request.GET.get('page')
  try:
   contacts = paginator.page(page)
  except PageNotAnInteger:
   contacts = paginator.page(1)
  except EmptyPage:
   contacts = paginator.page(paginator.num_pages)
  return render(request, 'Myapp/userlist.html', {'contacts': contacts})
 else:
  info = '暫無數(shù)據(jù)'
  return render(request, 'Myapp/userlist.html', {'info': info})

分頁的原理是用到了django自帶的分頁組件

需要注意的是,搜索的關(guān)鍵字傳參,用的是GET請求,而不是POST請求

簡單來說,就是先判斷前端頁面是否用搜索框搜索了關(guān)鍵字,如果搜索了,那么這個詞會被

key = request.GET.get('key')

賦值給key,然后進行后續(xù)判斷處理即可。

如果沒有,就正常展示我們需要展示的。

分頁的原理。

可以去看這個組件的文檔介紹。

如果返回的key檢索不到結(jié)果,返回空數(shù)據(jù)頁,并提醒。

html代碼:

{% load static %}
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!-->
<html lang="en"> <!--<![endif]-->
<head>
 
 <!-- favicon.ico
 ================================================== -->
 <link rel='shortcut icon' href="{% static 'Myapp/img/favicon.con' %}" rel="external nofollow" type="image/x-icon"/>
 
 <!-- Basic Page Needs
 ================================================== -->
 <meta charset="utf-8">
 <title>Detail</title>
 <meta name="description" content="">
 <meta name="author" content="">
 
 <!-- Mobile Specific Metas
	================================================== -->
 <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
 
 <!-- CSS
	================================================== -->
 <link rel="stylesheet" href="{% static 'Myapp/css/zerogrid.css' %}" rel="external nofollow" >
 <link rel="stylesheet" href="{% static 'Myapp/css/style.css' %}" rel="external nofollow" >
 <link rel="stylesheet" href="{% static 'Myapp/css/lightbox.css' %}" rel="external nofollow" >
 
 <!-- Custom Fonts -->
 <link href="{% static 'Myapp/assets/css/font-awesome.min.css' %}" rel="external nofollow" rel="stylesheet" type="text/css">
 
 
 <link rel="stylesheet" href="{% static 'Myapp/css/menu.css' %}" rel="external nofollow" >
 <link rel="stylesheet" href="{% static 'Myapp/assets/css/bootstrap.min.css' %}" rel="external nofollow" >
 <script src="{% static 'Myapp/js/jquery1111.min.js' %}" type="text/javascript"></script>
 <script src="{% static 'Myapp/js/script.js' %}"></script>
 
 <!--[if lt IE 8]>
  <div style=' clear: both; text-align:center; position: relative;'>
   <a  rel="external nofollow" >
   <img src="http://storage.ie6countdown.com/assets/100/images/banners/warning_bar_0000_us.jpg" border="0" height="42" width="820" alt="You are using an outdated browser. For a faster, safer browsing experience, upgrade for free today." />
  </a>
  </div>
 <![endif]-->
 <!--[if lt IE 9]>
		<script src="js/html5.js"></script>
		<script src="js/css3-mediaqueries.js"></script>
	<![endif]-->
 
</head>
 
<body>
<div class="wrap-body">
 
 <!--////////////////////////////////////Header-->
 <header class="zerogrid">
  <div class="logo"><img src="{% static 'Myapp/img/logo.png' %}" alt=""/></div>
  <div id='cssmenu' class="align-center">
   <ul>
    <li><a ><span>主頁</span></a></li>
    <li><a ><span>關(guān)于我們</span></a></li>
    <li><a ><span>每日看價</span></a></li>
    <li><a ><span>找個買家</span></a></li>
    <li class='last'><a ><span>聯(lián)系我們</span></a></li>
   </ul>
  </div>
  <form style="float: right">
   {% csrf_token %}
   <label>
    <div class="form-group">
     <input type="search" class="form-control" name="key" placeholder="您可輸入所在城市以檢索買家">
    </div>
   </label>
   <input class="btn btn-default" type="submit" value="搜索">
  </form>
 </header>
 
 <!--////////////////////////////////////Container-->
 <div style="text-align: center">
  <table style="float: contour; margin:0 auto;text-align: center; width: 800px;" class="table table-bordered">
   <tr style="text-align: center" class="success">
    <td>昵稱</td>
    <td>地址</td>
    <td>最近活躍</td>
   </tr>
   {% for u in contacts %}
    <tr class="info">
     <td><a
       href="http://127.0.0.1:8000/other/profile/{{ u.username }}" rel="external nofollow" >{{ u.username }}</a>
     </td>
     <td>{{ u.address }}</td>
     <td>{{ u.last_login }}</td>
    </tr>
   {% endfor %}
  </table>
 </div>
 
 
 <div class="navigation">
  <ul>
   {% for pg in contacts.paginator.page_range %}
    {% if contacts.number == pg %}
     <li class="active"><a href="?page={{ pg }}" rel="external nofollow" rel="external nofollow" >{{ pg }}</a></li>
    {% else %}
     <li><a href="?page={{ pg }}" rel="external nofollow" rel="external nofollow" >{{ pg }}</a></li>
    {% endif %}
   {% endfor %}
 
   {% if contacts.has_next %}
    <li><a href="?page={{ contacts.next_page_number }}" rel="external nofollow" >下一頁</a></li>
   {% endif %}
  </ul>
 </div>
 <!--////////////////////////////////////Footer-->
 
</div>
{% if info %}
 <script>
  window.alert('{{ info }}');
 </script>
{% endif %}
</body>
</html>

效果:

主頁主要功能部分:

信息頁分頁與搜索:搜索框和分頁都是存在的,目前是第一頁:http://127.0.0.1:8000/other/profile

第二頁:http://127.0.0.1:8000/other/profile?page=2

搜索測試:

搜索無效信息測試:

算是比較完整了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python基于stuck實現(xiàn)scoket文件傳輸

    Python基于stuck實現(xiàn)scoket文件傳輸

    這篇文章主要介紹了Python基于stuck實現(xiàn)scoket文件傳輸,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • Python?對象拷貝及深淺拷貝區(qū)別的詳細教程示例

    Python?對象拷貝及深淺拷貝區(qū)別的詳細教程示例

    這篇文章主要介紹了Python?對象拷貝及深淺拷貝區(qū)別的詳細教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • python簡單實現(xiàn)AES加密和解密

    python簡單實現(xiàn)AES加密和解密

    這篇文章主要為大家詳細介紹了python簡單實現(xiàn)AES加密和解密,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • Python中的index()方法使用教程

    Python中的index()方法使用教程

    這篇文章主要介紹了Python中的index()方法使用教程,是Python入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-05-05
  • keras實現(xiàn)多種分類網(wǎng)絡(luò)的方式

    keras實現(xiàn)多種分類網(wǎng)絡(luò)的方式

    這篇文章主要介紹了keras實現(xiàn)多種分類網(wǎng)絡(luò)的方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python函及模塊的使用

    Python函及模塊的使用

    這篇文章主要介紹了Python函及模塊的使用,基本函數(shù)包括定義函數(shù)、函數(shù)的參數(shù)、用模塊管理函數(shù)等一些基本定義,下面文章不僅對這些又說描述,還有變量的作用域的詳細內(nèi)容,需要的朋友可以參考一下,希望對你有所幫助
    2021-11-11
  • 在Pycharm中將pyinstaller加入External Tools的方法

    在Pycharm中將pyinstaller加入External Tools的方法

    今天小編就為大家分享一篇在Pycharm中將pyinstaller加入External Tools的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • 簡潔的十分鐘Python入門教程

    簡潔的十分鐘Python入門教程

    這篇文章主要介紹了簡潔的十分鐘Python入門教程,Python語言本身的簡潔也使得網(wǎng)絡(luò)上各種Python快門入門教程有著很高的人氣,本文是國內(nèi)此類其中的一篇,需要的朋友可以參考下
    2015-04-04
  • 日常整理python執(zhí)行系統(tǒng)命令的常見方法(全)

    日常整理python執(zhí)行系統(tǒng)命令的常見方法(全)

    本文是小編日常整理的些關(guān)于python執(zhí)行系統(tǒng)命令常見的方法,比較全面,特此通過腳本之家這個平臺把此篇文章分享給大家供大家參考
    2015-10-10
  • python socket 超時設(shè)置 errno 10054

    python socket 超時設(shè)置 errno 10054

    這篇文章主要介紹了python 遠程主機強迫關(guān)閉了一個現(xiàn)有的連接 socket 超時設(shè)置 errno 10054 ,需要的朋友可以參考下
    2014-07-07

最新評論