当前位置:网站首页>DRF - web development mode, API interface, API interface testing tool, restful specification, serialization and deserialization, DRF installation and use
DRF - web development mode, API interface, API interface testing tool, restful specification, serialization and deserialization, DRF installation and use
2022-07-29 00:46:00 【There is a car on the hill】
List of articles
List of articles
- List of articles
- Preface
- One 、web Development mode
- Two 、api Interface
- 3、 ... and 、api Interface testing tool
- Four 、restful standard
- 1. Use https Data interaction
- 2. Interface address with api identification
- 3. Identify the version ID in the interface address
- 4. Data is a resource , They all use nouns , Do not appear verbs
- 5. Resource operation is determined by the request mode
- 6. Address with search criteria
- 7. Response status code
- 8. There is an error message in the response
- 9. The result returned by the response should conform to the specification
- 5、 ... and 、 Serialization and deserialization
- 6、 ... and 、drf install
- 7、 ... and 、drf Quick to use
Preface
Learned before django Template layer of 、 The routing layer 、 View layer 、 The model layer , So let's start learning django Of drf Use of third-party plug-ins
One 、web Development mode
web Development falls into two broad categories :
1. Mixed development without separation of front and back ends
For hybrid development , The impact between front-end and back-end projects is great , Once the front-end or back-end changes, it will cause a chain reaction , Such development will make the efficiency insufficient , At the same time, it causes more bug The emergence of makes error checking more difficult to find .
2. Separate front and rear development
The separation of the front and rear ends makes , The front end and the back end perform their respective duties , Let's do our best . There is a change , Can be easily combined , Improve the development efficiency, and it is more convenient to troubleshoot errors .
Two 、api Interface
The medium of front and back information interaction
front end —》 Interface —》 Back end
front end 《— Interface 《— Back end
adopt api The interface converts the data type and sends it to the front-end or back-end implementation , Interaction between front and back ends
3、 ... and 、api Interface testing tool
postman Originally free software, but later began to charge , This software is highly used
postwoman imitation postman Developed software , It's open source and free
Aim at postwoman The installation process is basically the next step .
Four 、restful standard
To write api Interface in order to reduce api The use of , When writing, the mainstream is to follow restful Specification
1. Use https Data interaction
https Is based on http Based on the protocol tsl/ssl Encrypt data , While using api It is required to use https To interact with data
2. Interface address with api identification
To write api The interface needs to be marked as a api Interface instead of the corresponding file 、 Page etc.
for example :
https://api.baidu.com/books The identity is in the domain name
https://www.baidu.com/api The identity is in the address
3. Identify the version ID in the interface address
If a standard application adapts to the application of the old version, it needs to use the version to identify the corresponding api Interface
for example :
-https://api.baidu.com/v1 # Use here v Add numbers to indicate api edition
-https://api.weibo.com/2/statuses/user_timeline.json # Numbers are used here to indicate api edition
4. Data is a resource , They all use nouns , Do not appear verbs
Writing api Interface is no longer url Use the verb , avoid api Repeated compilation of interfaces , Can also increase api Interface security
for example :
https://api.baidu.com/v1/user_login # This is wrong
https://api.baidu.com/v1/user # This writing is correct
5. Resource operation is determined by the request mode
For the same interface, it needs to produce multiple functions , This requires the request mode to decide what function to call
for example :
https://api.baidu.com/v1/user Use post Request to add data
https://api.baidu.com/v1/user Use pdelete Request to delete data
https://api.baidu.com/v1/user Use put Request to modify the data
https://api.baidu.com/v1/user Use get Request to query the data
6. Address with search criteria
When we need corresponding parameters to send a request, we can directly send it in the corresponding request url You can carry it after
for example :
https://api.baidu.com/v1/user?username=bbs123&user_id=1
7. Response status code
The response status code can make the development process clear and know the problem
for example :
1XX The request is being processed
2XX The request is successful Common are 200 The request is successful 201 Create success
3XX Redirect Common are 301 Temporary redirection 302 Permanent redirection
4XX Client error Common are 403 No omnidirectional 404 The resource address is not used
5XX Server error Common are 500 Encountered a request that I don't know how to handle 501 The requested method is not supported 502 The server cannot respond to the request
8. There is an error message in the response
When the request is successfully triggered , The response returned by the server will be accompanied by the corresponding response status code and the corresponding status information
for example :
{
'code':200, 'msg':' Registered successfully '}
9. The result returned by the response should conform to the specification
for example :
GET /collection: Returns a list of resource objects ( Array )
The result returned by the response :[{
'food_name':' Apple ', 'num':5,'price':12},{
'food_name':' pears ', num:8,price:12}]
GET /collection/resource: Return a single resource object
The result returned by the response :{
'food_name':' Apple ', 'num':5,'price':12}
POST /collection: Return the newly generated resource object
The result returned by the response :{
'food_name':' Apple ', 'num':5,'price':12}
PUT /collection/resource: Returns the complete resource object
The result returned by the response :{
'food_name':' Apple ', 'num':5,'price':12}
PATCH /collection/resource: Returns the complete resource object
The result returned by the response :{
'food_name':' Apple ', 'num':5,'price':12}
DELETE /collection/resource: Return an empty document
The result returned by the response :[]
5、 ... and 、 Serialization and deserialization
Serialization refers to converting the written content into a format that the receiver can parse
Deserialization refers to the conversion of data , Through the conversion method given by the sender, the direction is resolved to the data format that the receiver can use
At this time, the information transmission between the front and back ends can be realized through serialization and deserialization
Serialization and deserialization are also api Key knowledge of interface
6、 ... and 、drf install
drf Namely djangorestframework Abbreviation , It is djagno One of the app( Third party plug-ins ), Can only be used in django On the frame
drf It can help developers write quickly api Interface
drf and django The version of has a certain correspondence
Mode one
pip3 install djangorestframework
Mode two :
stay pycharm Search for installation
7、 ... and 、drf Quick to use
The model layer
models.py
from django.db import models
# Create your models here.
class Book(models.Model):
name=models.CharField(max_length=32)
price=models.IntegerField()
drf Registration of
serializer.py
from rest_framework import serializers
from .models import Book
class BookSerializer(serializers.ModelSerializer):
class Meta:
model=Book
fields='__all__'
View layer
views.py
from django.shortcuts import render
# Create your views here.
from rest_framework.viewsets import ModelViewSet
from .models import Book
from .serializer import BookSerializer
class BookView(ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
Then you can call api Interface to directly add, delete, modify and query
for example :
https://127.0.0.1:8080/book # At this time to use get You can get all the book information objects
https://127.0.0.1:8080/book
#formdata Carry the corresponding book title and price in use post You can add books by requesting
边栏推荐
猜你喜欢

PTA (daily question) 7-75 how many people in a school

Api 接口优化有哪些技巧?

What does the expression > > 0 in JS mean

最长上升子序列

芯驰科技发布G9系列最新旗舰产品,配备6个1.8Ghz主频的A55核心

Teach you how to install latex (nanny level tutorial)

PTA (daily question) 7-73 turning triangle

Software designer afternoon question

从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析

第二轮1000个Okaleido Tiger,再次登录Binance NFT 1小时售罄
随机推荐
Flash and seven cattle cloud upload pictures
Jupyter notebook中5个有趣的魔法命令
PTA (daily question) 7-72 calculate the cumulative sum
Alibaba code index technology practice: provide reading experience of local IDE for code review
2022DASCTF7月赋能赛(复现)
R语言怎么学
乱打日志的男孩运气怎么样我不知道,加班肯定很多!
(20211130更新)关于jupyter notebook的下载安装及自己的配置、主题
Data warehouse construction - DWT floor
Basic knowledge of PHP language (super detailed)
【esn】 学习回声状态网络
What does the expression > > 0 in JS mean
Dynamic programming problem (VIII)
NPM run serve stuck at 40%
execute immediate 简单示例合集(DML)
【愚公系列】2022年07月 Go教学课程 020-Go容器之数组
1331. Array sequence number conversion: simple simulation question
Api 接口优化有哪些技巧?
Shell programming specifications and variables
【MySQL 8】Generated Invisible Primary Keys(GIPK)