使用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):
# 獲取當(dāng)前頁碼數(shù)
num = request.GET.get('num', 1)
n = int(num)
# 1.查詢stu_student表中的所有數(shù)據(jù)
stus = Student.objects.all() # 獲取所有的
# django 分頁
pager = Paginator(stus, 2)
# 獲取當(dāng)前頁面的數(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)文章
對python:threading.Thread類的使用方法詳解
今天小編就為大家分享一篇對python:threading.Thread類的使用方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
Python使用Beautiful Soup實現(xiàn)解析網(wǎng)頁
在這篇文章中,我們將介紹如何使用 Python 編寫一個簡單的網(wǎng)絡(luò)爬蟲,以獲取并解析網(wǎng)頁內(nèi)容。我們將使用 Beautiful Soup 庫,它是一個非常強大的庫,用于解析和操作 HTML 和 XML 文檔。讓我們開始吧2023-05-05
python中torch.load中的map_location參數(shù)使用
在PyTorch中,torch.load()函數(shù)是用于加載保存模型或張量數(shù)據(jù)的重要工具,map_location參數(shù)為我們提供了極大的靈活性,具有一定的參考價值,感興趣的可以了解一下2024-03-03
Python 排序最長英文單詞鏈(列表中前一個單詞末字母是下一個單詞的首字母)
這篇文章主要介紹了Python 排序最長英文單詞鏈(列表中前一個單詞末字母是下一個單詞的首字母),列表中每個元素相當(dāng)于一個單詞,要實現(xiàn)列表中前一個單詞末字母是下一個單詞的首字母,并且這個鏈?zhǔn)亲铋L的。感興趣的可以了解一下2020-12-12
Python計算標(biāo)準(zhǔn)差之numpy.std和torch.std的區(qū)別
Torch自稱為神經(jīng)網(wǎng)絡(luò)中的numpy,它會將torch產(chǎn)生的tensor放在GPU中加速運算,就像numpy會把array放在CPU中加速運算,下面這篇文章主要給大家介紹了關(guān)于Python?Numpy計算標(biāo)準(zhǔn)差之numpy.std和torch.std區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-08-08

