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

Components used in Django rest framework

編輯:Python

1、Authentication: Certification

Global authentication can be provided or set in a specific view authentication_classes Class attribute to set .

2、Permissions: Permission class

In the inherited APIView Defined in the class view of permission_classes = [permissions.IsAuthenticated]

AllowAny Allow all users
IsAuthenticated Only users authenticated by login
IsAdminUser Only administrator users
IsAuthenticatedOrReadOnly Users who have logged in for authentication can add, delete and modify data , No login authentication can only view data .

3、Throttling: Current limiting class

You can limit the frequency of interface access , To reduce server pressure , Or implement a specific business .

4、Filtering: Filter class

The list data may need to be filtered according to the fields , We can add django-fitlter Extend to enhance support .

5、OrderingFilter: Sorting class

For list data ,REST framework Provides OrderingFilter Filter to help us quickly indicate that the data is sorted according to the specified field

6、Pagination: Paging class

REST framework Provides paging support . We can set the global paging mode in the configuration file . You can also customize Pagination class ( Inherit PageNumberPagination), To add different pagination behaviors to the view . In the view through pagination_class Attribute to indicate .

7、Exceptions: Exception handling class

REST framework Provides a default routine , We can also customize the way to write exception handling functions . In the configuration file of the main application settings.py Declare custom exception handling in .

DRF Default exception in

ParseError Parse error
AuthenticationFailed Authentication failed
NotAuthenticated Not yet certified
PermissionDenied Limited authority
NotFound Route not found
MethodNotAllowed Request mode not supported
NotAcceptable The data format to get is not supported
Throttled The number of times the current limit is exceeded
ValidationError Check failed

8、 Automatically generate interface documentation :

DRF Need to install coreapi Library support .

pip install coreapi

1) Set interface document access path

REST_FRAMEWORK = {
 # ... The other options # Interface document 
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema', }

2) Routing configuration

from rest_framework.documentation import include_docs_urls
urlpatterns = [
...
path('docs/', include_docs_urls(title=' Site page title '))
]

3) Access the interface documentation page

Browser access 127.0.0.1:8000/docs/, You can see the automatically generated interface document .


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