Implementation of returning data using Django’s jsonresponse

Time:2021-9-20

urls.py

  1. from django.conf.urls import url
  2. from . import views
  3.  
  4. urlpatterns = [
  5. url(r’^show/’, views.show_view, name=’show’)
  6. ]

Create show in views.py_ View function

  1. from django.http import HttpResponse
  2. from django.shortcuts import render
  3. from .models import *
  4. from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
  5. from django.http import JsonResponse
  6. def show_view(request):
  7. #Gets the current number of page numbers
  8. num = request.GET.get(‘num’, 1)
  9. n = int(num)
  10. #1. Query stu_ All data in the student table
  11. Stus = student. Objects. All() # get all
  12. #Django paging
  13. pager = Paginator(stus, 2)
  14. #Get the data of the current page
  15. try:
  16. stuss = Student.objects.all().values()
  17. students = list(stuss)
  18. return JsonResponse({‘code’: 200, ‘data’: students})
  19. perpage_data = pager.page(n)
  20. #Returns the data of the first page
  21. except PageNotAnInteger:
  22. perpage_data = pager.page(1)
  23. #Returns the data of the last page
  24. except EmptyPage:
  25. perpage_data = pager.page(pager.num_pages)
  26. return render(request, ‘show.html’, {‘show’: stus, ‘pager’: pager, ‘perpage_data’: perpage_data})

Implementation of returning data using Django's jsonresponse

Implementation of returning data using Django's jsonresponse

This is the end of this article about the implementation of using Django’s jsonresponse to return data. For more information about Django’s jsonresponse, please search previous articles on developeppaper or continue to browse the following articles. I hope you will support developeppaper in the future!

Recommended Today

Supervisor

Supervisor [note] Supervisor – H view supervisor command help Supervisorctl – H view supervisorctl command help Supervisorctl help view the action command of supervisorctl Supervisorctl help any action to view the use of this action 1. Introduction Supervisor is a process control system. Generally speaking, it can monitor your process. If the process exits abnormally, […]