程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

使用Django的JsonResponse返回數據

編輯:Python

urls.py

from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^show/', views.show_view, name='show')
]

在views.py中創建show_view函數

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):
# 獲取當前頁碼數
num = request.GET.get('num', 1)
n = int(num)
# 1.查詢stu_student表中的所有數據
stus = Student.objects.all() # 獲取所有的
# django 分頁
pager = Paginator(stus, 2)
# 獲取當前頁面的數據
try:
stuss = Student.objects.all().values()
students = list(stuss)
return JsonResponse({'code': 200, 'data': students})
perpage_data = pager.page(n)
# 返回第一頁的數據
except PageNotAnInteger:
perpage_data = pager.page(1)
# 返回最後一頁的數據
except EmptyPage:
perpage_data = pager.page(pager.num_pages)
return render(request, 'show.html', {'show': stus, 'pager': pager, 'perpage_data': perpage_data})

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved