Write your own API in Django

Source: Internet
Author: User
Tags install django pip install django

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.