使用Django的JsonResponse返回數(shù)據(jù)的實現(xiàn)
urls.py
from django.conf.urls import url from . import views urlpatterns = [ url(r'^show/', views.show_view, name='show') ]
在views.py中創(chuàng)建show_view函數(shù)
from django.http import HttpResponse from django.shortcuts import render from .models import * from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage from django.http import JsonResponse def show_view(request): # 獲取當前頁碼數(shù) num = request.GET.get('num', 1) n = int(num) # 1.查詢stu_student表中的所有數(shù)據(jù) stus = Student.objects.all() # 獲取所有的 # django 分頁 pager = Paginator(stus, 2) # 獲取當前頁面的數(shù)據(jù) try: stuss = Student.objects.all().values() students = list(stuss) return JsonResponse({'code': 200, 'data': students}) perpage_data = pager.page(n) # 返回第一頁的數(shù)據(jù) except PageNotAnInteger: perpage_data = pager.page(1) # 返回最后一頁的數(shù)據(jù) except EmptyPage: perpage_data = pager.page(pager.num_pages) return render(request, 'show.html', {'show': stus, 'pager': pager, 'perpage_data': perpage_data})
到此這篇關(guān)于使用Django的JsonResponse返回數(shù)據(jù)的實現(xiàn)的文章就介紹到這了,更多相關(guān)Django JsonResponse內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于python中不同函數(shù)讀取圖片格式的區(qū)別淺析
這篇文章主要給大家介紹了關(guān)于python中不同函數(shù)讀取圖片格式的區(qū)別,文中通過實例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下2022-03-03Python抓取數(shù)據(jù)到可視化全流程的實現(xiàn)過程
這篇文章主要介紹了Python抓取數(shù)據(jù)到可視化全流程的實現(xiàn)過程,2022-01-01python實現(xiàn)可視化動態(tài)CPU性能監(jiān)控
這篇文章主要為大家詳細介紹了python可視化動態(tài)CPU性能監(jiān)控,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06Python常用數(shù)據(jù)類型之間的轉(zhuǎn)換總結(jié)
在本篇文章里小編給大家整理的是關(guān)于Python中常用數(shù)據(jù)類型之間的轉(zhuǎn)換相關(guān)知識點,有需要的朋友們可以學(xué)習下2019-09-09Python實現(xiàn)GUI學(xué)生信息管理系統(tǒng)
這篇文章主要為大家詳細介紹了Python實現(xiàn)GUI學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01