Get JSON data from a specific URL--and serialize the JSON data in the dictionary format--and pass the dictionary format data to the template by deserializing the JSON data
Tool: Rest_frawork
Rest_frawork Library Installation-->PIP installation python3-m pip install django-rest-framework--add rest_framework to App
From website.models import Video #获取json数据的model
From rest_framework import serializers
From Rest_framework.response Import response
From rest_framework.decorators import Api_view
Class Viedoseriazer (serializers. Modelserializer): #创建序列化器
Class Meta:
Model = Video #序列化对象为Viedo模型
Fields = ' __all__ ' #所有字段
@api_view ([' GET ']) #装饰器 decorate the data in JSON format
def video (Request):
Video_list = Video.objects.all ()
Serializer = Viedoseriazer (video_list,many=true) #将得到的数据序列化
Return Response (Serializer.data) #此时得到的是一个半序列化的数据
--At this point, the Django creates an API interface, and then it's the most fun to call it.
Write your own API in Django