最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

代碼如下:

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

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):
 # 實(shí)現(xiàn)搜索
 key = request.GET.get('key')
 all_users = UserInfo.objects.filter(cate="買家").all()
 userlist = []
 # 如果前端傳入關(guān)鍵字,才會(huì)進(jì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請(qǐng)求,而不是POST請(qǐng)求

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

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

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

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

分頁的原理。

可以去看這個(gè)組件的文檔介紹。

如果返回的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>每日看價(jià)</span></a></li>
    <li><a ><span>找個(gè)買家</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

搜索測(cè)試:

搜索無效信息測(cè)試:

算是比較完整了。

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

相關(guān)文章

  • 使用Python在Excel中插入、修改、提取和刪除超鏈接

    使用Python在Excel中插入、修改、提取和刪除超鏈接

    超鏈接是Excel中的常用功能,通過點(diǎn)擊超鏈接可以快速跳轉(zhuǎn)到外部網(wǎng)站、本地文件或工作表中的特定單元格,有效提升數(shù)據(jù)訪問的效率和用戶體驗(yàn),這篇博客將詳細(xì)介紹如何使用Python在Excel中插入、修改、提取和刪除超鏈接,需要的朋友可以參考下
    2025-02-02
  • python基于gevent實(shí)現(xiàn)并發(fā)下載器代碼實(shí)例

    python基于gevent實(shí)現(xiàn)并發(fā)下載器代碼實(shí)例

    這篇文章主要介紹了python基于gevent實(shí)現(xiàn)并發(fā)下載器代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Python實(shí)現(xiàn)批量將word轉(zhuǎn)換成pdf

    Python實(shí)現(xiàn)批量將word轉(zhuǎn)換成pdf

    這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)批量將word文檔轉(zhuǎn)換成pdf文件,文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-08-08
  • Python Word文件自動(dòng)化實(shí)戰(zhàn)之簡(jiǎn)歷篩選

    Python Word文件自動(dòng)化實(shí)戰(zhàn)之簡(jiǎn)歷篩選

    本文將利用Python自動(dòng)化做一個(gè)具有實(shí)操性的小練習(xí),即通過讀取簡(jiǎn)歷來篩選出符合招聘條件的簡(jiǎn)歷。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-05-05
  • python中終止協(xié)程和異常處理方式

    python中終止協(xié)程和異常處理方式

    這篇文章主要介紹了python中終止協(xié)程和異常處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Python入門教程2. 字符串基本操作【運(yùn)算、格式化輸出、常用函數(shù)】

    Python入門教程2. 字符串基本操作【運(yùn)算、格式化輸出、常用函數(shù)】

    這篇文章主要介紹了Python字符串基本操作,結(jié)合實(shí)例形式總結(jié)分析了Python字符串的基本表示、運(yùn)算、格式化輸出、常用函數(shù)等,注釋包含較為詳盡的說明,便于理解,需要的朋友可以參考下
    2018-10-10
  • Django 解決阿里云部署同步數(shù)據(jù)庫報(bào)錯(cuò)的問題

    Django 解決阿里云部署同步數(shù)據(jù)庫報(bào)錯(cuò)的問題

    這篇文章主要介紹了Django 解決阿里云部署同步數(shù)據(jù)庫報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • python安裝pandas庫不成功原因分析及解決辦法

    python安裝pandas庫不成功原因分析及解決辦法

    Pandas是python中非常常用的數(shù)據(jù)分析庫,在數(shù)據(jù)分析、機(jī)器學(xué)習(xí)、深度學(xué)習(xí)等領(lǐng)域經(jīng)常被使用,下面這篇文章主要給大家介紹了關(guān)于python安裝pandas庫不成功原因分析及解決辦法的相關(guān)資料
    2023-11-11
  • 使用Python讀取二進(jìn)制文件的實(shí)例講解

    使用Python讀取二進(jìn)制文件的實(shí)例講解

    今天小編就為大家分享一篇使用Python讀取二進(jìn)制文件的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • 使用Pandas進(jìn)行Excel數(shù)據(jù)處理的操作和技巧

    使用Pandas進(jìn)行Excel數(shù)據(jù)處理的操作和技巧

    在數(shù)據(jù)處理和分析的過程中,Excel是一個(gè)非常常見的工具,然而,當(dāng)數(shù)據(jù)量變大,操作復(fù)雜度增加時(shí),Excel的效率和功能可能無法滿足需求,Pandas是一個(gè)強(qiáng)大的Python數(shù)據(jù)處理庫,本文將介紹如何使用Pandas進(jìn)行Excel數(shù)據(jù)處理,并展示一些常見的操作和技巧
    2023-11-11

最新評(píng)論

赣州市| 威远县| 定结县| 辰溪县| 和龙市| 鹤峰县| 福建省| 射阳县| 财经| 若尔盖县| 隆子县| 阿拉善左旗| 瓮安县| 宜昌市| 广宗县| 中江县| 东辽县| 凤山县| 石棉县| 波密县| 工布江达县| 奎屯市| 东安县| 乐亭县| 北安市| 滦南县| 湘潭市| 衡东县| 措勤县| 辉县市| 乾安县| 达州市| 淅川县| 南陵县| 建昌县| 历史| 霸州市| 杂多县| 西安市| 延长县| 平昌县|