Python學習第十九天

Django-分頁

后端分頁

????????Django提供了Paginator類來實現后端分頁。Paginator類可以將一個查詢集(QuerySet)分成多個頁面,每個頁面包含指定數量的對象。

from django.shortcuts import render, redirect, get_object_or_404
from .models import User
from .forms import UserForm
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger'''用戶列表
'''
def user_list(request):user_list = User.objects.all()# 處理自定義值try:# 獲取用戶選擇的每頁顯示數量,默認為 10per_page = int(request.GET.get('per_page', 10))per_page = int(per_page)  # 將 per_page 轉換為整數if per_page <= 0:  # 如果輸入的值小于等于0,設置為默認值10per_page = 10except (ValueError, TypeError):  # 如果 per_page 不是數字,設置為默認值10per_page = 10# 分頁paginator = Paginator(user_list, per_page)page_number = request.GET.get('page')try:page_obj = paginator.get_page(page_number)  # 獲取當前頁except PageNotAnInteger:  # 如果 page 不是整數,跳轉到第一頁page_obj = paginator.get_page(1)except EmptyPage:  # 如果 page 超出范圍,跳轉到最后一頁page_obj = paginator.get_page(paginator.num_pages)# 允許的每頁顯示數量列表allowed_per_page = [10, 20, 50, 100, 200]return render(request, 'myapp/user_list.html', {'page_obj': page_obj,'per_page': per_page,  # 將每頁顯示數量傳遞給模板'allowed_per_page': allowed_per_page,})

前端分頁

? ? ? 通用前端的分頁代碼支持輸入頁碼,也支持選擇頁碼,跳轉到對應頁面

<nav aria-label="Page navigation" class="mt-4"><ul class="pagination justify-content-center"><!-- 首頁按鈕 -->{% if page_obj.has_previous %}<li class="page-item"><a class="page-link" href="?page=1&per_page={{ per_page }}" aria-label="First"><span aria-hidden="true">首頁</span></a></li>{% else %}<li class="page-item disabled"><span class="page-link">首頁</span></li>{% endif %}<!-- 上一頁按鈕 -->{% if page_obj.has_previous %}<li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}&per_page={{ per_page }}" aria-label="Previous"><span aria-hidden="true">上一頁</span></a></li>{% else %}<li class="page-item disabled"><span class="page-link">上一頁</span></li>{% endif %}<!-- 當前頁碼信息 --><li class="page-item active"><span class="page-link">第 {{ page_obj.number }} 頁,共 {{ page_obj.paginator.num_pages }} 頁</span></li><!-- 下一頁按鈕 -->{% if page_obj.has_next %}<li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}&per_page={{ per_page }}" aria-label="Next"><span aria-hidden="true">下一頁</span></a></li>{% else %}<li class="page-item disabled"><span class="page-link">下一頁</span></li>{% endif %}<!-- 末頁按鈕 -->{% if page_obj.has_next %}<li class="page-item"><a class="page-link" href="?page={{ page_obj.paginator.num_pages }}&per_page={{ per_page }}" aria-label="Last"><span aria-hidden="true">末頁</span></a></li>{% else %}<li class="page-item disabled"><span class="page-link">末頁</span></li>{% endif %}</ul><!-- 輸入頁碼跳轉和選擇每頁顯示數量 --><div class="d-flex justify-content-center mt-3"><!-- 輸入頁碼跳轉 --><form method="get" action="" class="form-inline mr-3"><div class="input-group"><input type="number" name="page" class="form-control" min="1" max="{{ page_obj.paginator.num_pages }}"placeholder="頁碼" aria-label="頁碼" required><input type="hidden" name="per_page" value="{{ per_page }}"><div class="input-group-append"><button type="submit" class="btn btn-outline-primary">跳轉</button></div></div></form><!-- 選擇每頁顯示數量 --><form method="get" action="" class="form-inline"><div class="input-group"><select name="per_page" class="form-control" aria-label="每頁顯示數量" onchange="this.form.submit()"><option value="10" {% if per_page == 10 %}selected{% endif %}>10 條/頁</option><option value="20" {% if per_page == 20 %}selected{% endif %}>20 條/頁</option><option value="50" {% if per_page == 50 %}selected{% endif %}>50 條/頁</option><option value="100" {% if per_page == 100 %}selected{% endif %}>100 條/頁</option><option value="200" {% if per_page == 200 %}selected{% endif %}>200 條/頁</option><option value="1" {% if per_page != 10 and per_page != 20 and per_page != 50 and per_page != 100 and per_page != 200 %}selected{% endif %}>自定義</option></select>{% if per_page != 10 and per_page != 20 and per_page != 50 and per_page != 100 and per_page != 200 %}<input type="number" name="per_page" class="form-control ml-2" min="1"value="{{ per_page }}" placeholder="自定義" aria-label="自定義" required>{% endif %}</div></form></div>
</nav>

靜態資源配置

使用

在前端中需要引入背景圖或者其他的靜態資源

配置settings.py

# 靜態資源
STATIC_URL = 'static/'
import os
# 靜態文件目錄
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),           # 全局靜態文件目錄os.path.join(BASE_DIR, 'user/static'),     # user app 的靜態文件目錄
]

目錄以及加載使用

html中使用

<!-- 加載靜態文件 -->
{% load static %}
<!-- 全局靜態文件 -->
<!--<body style="background-image: url('{% static '02.png' %}');">-->
<!-- 引入app下的的靜態文件 --><body style="background-image: url('{% static 'user/images/backgroud_image.png' %}');">

防止多次加載

????????在 Django 模板中,使用?{% block %}?來定義可覆蓋的區域,避免在多個模板中重復加載相同的靜態資源。(主要是使用這個其他通過繼承這部分內容 有點像是iframe標簽 引入了部分代碼塊)

# 在main.html中使用{% block content %}{% endblock %}<!-- 加載靜態文件 -->
{# main.html注釋哈 #}
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>{% block title %}首頁{% endblock %}</title>
</head>
<!-- 全局靜態文件 -->
<!--<body style="background-image: url('{% static '02.png' %}');">-->
<!-- 引入app下的的靜態文件 比如使用app下的靜態資源不需要重復引入 -->
<body style="background-image: url('{% static 'user/images/backgroud_image.png' %}');">
<a href="{% url 'main' %}">首頁</a>
<a href="{% url 'register' %}">注冊</a>
<a href="{% url 'login' %}">登錄</a>
{# 主要是使用這個其他通過繼承這部分內容 有點像是iframe標簽 引入了部分代碼塊 #}
{% block content %}{% endblock %}
</body>
</html>

子類通過繼承{% extends 'main.html' %} 以及主類中的{% block 名稱%}

{# login.html的模版 繼承main的靜態資源只需要寫自己的相關代碼塊即可 #}
{% extends 'main.html' %}{% block content %}
用戶名:<input name="username" value ="" placeholder="請輸入用戶名"/>
密碼:<input name="password" value ="" placeholder="請輸入密碼"/>
{% endblock%}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/73616.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/73616.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/73616.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Windows下安裝Git客戶端

① 官網地址&#xff1a;https://git-scm.com/。 ② Git的優勢 大部分操作在本地完成&#xff0c;不需要聯網&#xff1b;完整性保證&#xff1b;盡可能添加數據而不是刪除或修改數據&#xff1b;分支操作非常快捷流暢&#xff1b;與Linux 命令全面兼容。 ③ Git的安裝 從官網…

刷題練習筆記

目錄 1、消失的數字 2、旋轉數組 3、原地移除元素 4、刪除排序數組中的重復項 1、消失的數字 oj&#xff1a;面試題 17.04. 消失的數字 - 力扣&#xff08;LeetCode&#xff09; 思路&#xff08;參考評論區&#xff09;&#xff1a; 利用異或的特性&#xff0c;ret ret …

C或C++中實現數據結構課程中的鏈表、數組、樹和圖案例

1. 雙向鏈表&#xff08;Doubly Linked List&#xff09;-----支持雙向遍歷。 C實現 #include <iostream>struct Node {int data;Node* prev;Node* next; };class DoublyLinkedList { private:Node* head; public:DoublyLinkedList() : head(nullptr) {}// 在鏈表末尾插…

94.HarmonyOS NEXT動畫系統實現教程:深入理解FuncUtils

溫馨提示&#xff1a;本篇博客的詳細代碼已發布到 git : https://gitcode.com/nutpi/HarmonyosNext 可以下載運行哦&#xff01; HarmonyOS NEXT動畫系統實現教程&#xff1a;深入理解FuncUtils 文章目錄 HarmonyOS NEXT動畫系統實現教程&#xff1a;深入理解FuncUtils1. 動畫系…

AI日報 - 2025年3月17日

&#x1f31f; 今日概覽&#xff08;60秒速覽&#xff09; ▎&#x1f916; AGI突破 | GPT-o1在卡內基梅隆大學數學考試中獲滿分&#xff0c;展示AI數學能力新高度 成本僅5美分/題&#xff0c;推理速度不到1分鐘 ▎&#x1f4bc; 商業動向 | Figure推出BotQ機器人制造設施&…

Tauri + Vite + SvelteKit + TailwindCSS + DaisyUI 跨平臺開發詳細配置指南(Windows)

Tauri Vite SvelteKit TailwindCSS DaisyUI 跨平臺開發詳細配置指南&#xff08;Windows&#xff09; 本文為博主原創文章&#xff0c;遵循 CC 4.0 BY-SA 版權協議。轉載請注明出處及本聲明 原文鏈接&#xff1a;[你的文章鏈接] &#x1f6e0;? 環境準備 1. 安裝核心工具…

在 macOS 上優化 Vim 用于開發

簡介 這篇指南將帶你通過一系列步驟&#xff0c;如何在 macOS 上優化 Vim&#xff0c;使其具備 代碼補全、語法高亮、代碼格式化、代碼片段管理、目錄樹等功能。此外&#xff0c;我們還會解決在安裝過程中可能遇到的常見錯誤。 1. 安裝必備工具 在開始 Vim 配置之前&#xff…

golang開發支持onlyoffice的token功能

一直都沒去弄token這塊&#xff0c;想著反正docker run的時候將jwt置為false即可。 看了好多文章&#xff0c;感覺可以試試&#xff0c;但是所有文件幾乎都沒說思路。 根據我的理解和成功的調試&#xff0c;思路是&#xff1a; 我們先定義2個概念&#xff0c;一個是文檔下載…

Android wifi的開關Settings值異常分析

Android wifi的開關Settings值異常分析 文章目錄 Android wifi的開關Settings值異常分析一、前言二、異常分析1、adb或者串口獲取Settings的wifi開關值2、代碼獲取wifi開關值3、根據日志分析代碼(1)logcat 對應的wifi開啟日志的代碼①WifiServiceImpl.java② WifiSettingsStore…

C#的委托Action

在 C# 中&#xff0c;Action 是一個預定義的委托類型&#xff0c;它位于 System 命名空間下。下面詳細介紹它的作用和使用方法。 作用 Action 委托的主要作用是封裝一個方法&#xff0c;這個方法沒有返回值&#xff08;即返回類型為 void&#xff09;。它提供了一種簡潔的方式…

Qt MainWindow簡單例子(文本編輯)

使用Qt控件練習文本編輯窗口的創建。 #ifndef MAINWINDOW_H #define MAINWINDOW_H#include <QMainWindow> #include <QLabel> #include <QProgressBar>QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACEclass MainWindow : public Q…

DeepSeek-prompt指令-當DeepSeek答非所問,應該如何準確的表達我們的訴求?

當DeepSeek答非所問&#xff0c;應該如何準確的表達我們的訴求&#xff1f;不同使用場景如何向DeepSeek發問&#xff1f;是否有指令公式&#xff1f; 目錄 1、 扮演專家型指令2、 知識蒸餾型指令3、 顆粒度調節型指令4、 時間軸推演型指令5、 極端測試型6、 逆向思維型指令7、…

Mac中nvm切換node版本失敗,關閉終端再次打開還是之前的node

Mac中使用 nvm 管理 node 版本&#xff0c;在使用指令&#xff1a;nvm use XXX 切換版本之后。 關閉終端&#xff0c;再次打開&#xff0c;輸入 node -v 還是得到之前的 node 版本。 原因&#xff1a; 在這里這個 default 中有個 node 的版本號&#xff0c;使用 nvm use 時&a…

織夢dedecmsV5.7提示信息提示框美化(帶安裝教程和效果展示)

一、效果展示 1、安裝前效果 2、安裝后效果 二、安裝說明 1、安裝測試版本&#xff1a;DedeCMS-V5.7.117-UTF8&#xff1b; 2、必須在修改代碼之前請做好文件備份&#xff0c;以免誤操無法恢復&#xff1b; 3、為了兼容其他版本&#xff0c;請在安裝時&#xff0c;最好將替…

Science Advances 視觸覺傳感機制的交互裝置,可以實時測量來自手不同部位的分布力

近日&#xff0c;由香港科技大學&#xff08;HKUST&#xff09;電子與計算機工程學系申亞京教授領導的研究團隊&#xff0c;提出了一種基于數字通道的觸覺交互系統&#xff0c;可以實時測量來自手不同部位的分布力&#xff0c;有望在醫學評估、體育訓練、機器人和虛擬現實&…

MySQL單表查詢大全【SELECT】

山再高&#xff0c;往上攀&#xff0c;總能登頂&#xff1b;路再長&#xff0c;走下去&#xff0c;定能到達。 Mysql中Select 的用法 ------前言------【SELECT】0.【準備工作】0.1 創建一個庫0.2 庫中創建表0.3 表中加入一些數據 1.【查詢全部】2.【查詢指定列】2.1查詢指定列…

Vue調用子組件init方法時報錯Cannot read properties of undefined (reading ‘init‘)解決方法

調用init方法語句寫在this.$nextTick(() > {});方法里&#xff0c;因為nextTick方法在頁面元素加載完之后調用 this.$nextTick(() > {this.$refs.chartComponent.init();});如果還報錯&#xff1a;Error in nextTick: "TypeError: Cannot read properties of undef…

怎么解決在Mac上每次打開文件夾都會彈出一個新窗口的問題

在Mac上每次打開文件夾都會彈出一個新窗口的問題&#xff0c;可以通過以下方法解決? ?調整Finder設置?&#xff1a; 打開Finder&#xff0c;點擊“Finder”菜單&#xff0c;選擇“偏好設置”。在偏好設置中&#xff0c;選擇“通用”標簽。取消勾選“在標簽頁中打開文件夾”或…

從 Prop Drilling 到 Context:React 狀態管理的演進與抉擇

Context的出現解決了什么問題&#xff1f; Vue中的provide/inject和React中的Context非常相似&#xff0c;具體區別如下&#xff1a; 可以看到實際上最大的區別在于Vue是響應式&#xff0c;React是非響應式 那么context具體解決了什么問題&#xff1f;我們先看下面這個例子&a…

考研408-數據結構完整代碼 線性表的順序存儲結構 - 順序表

線性表的順序存儲結構 - 順序表 1. 順序表的定義 ? 用一組地址連續的存儲單元依次存儲線性表的數據元素&#xff0c;從而使邏輯上相鄰的兩個元素在物理位置上也相鄰 2. 順序表的特點 隨機訪問&#xff1a; 即通過首地址和元素序號可以在O(1) 時間內找到指定元素&#xff0…