Global authentication can be provided or set in a specific view authentication_classes Class attribute to set .
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 .
You can limit the frequency of interface access , To reduce server pressure , Or implement a specific business .
The list data may need to be filtered according to the fields , We can add django-fitlter Extend to enhance support .
For list data ,REST framework Provides OrderingFilter Filter to help us quickly indicate that the data is sorted according to the specified field
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 .
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
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 .