Routing control, versioning, parser, responder

Routing Control

The basic routing wording

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^register/', views.register),
    url(r'^login/', views.Login.as_view()),
    url(r'^test/', views.Authtest.as_view()),
]

 

The second wording

Must inherit just inherited ViewSetMixin:

urlpatterns = [
           url(r'^publish/$', views.PublishView.as_view({'get':'list','post':'create'})),
           url(r'^publish\.(?P<format>\w+)$', views.PublishView.as_view({'get':'list','post':'create'})),
           url(r'^publish/(?P<pk>\d+)$',views.PublishView.as_view({'get':'retrieve','delete':'destroy','put':'update'})),
]

 

The third

Automatic route generation, must inherit ModelViewSet, essentially inherited ViewSetMixin:

            #SimpleRouter 自动生成两条路由
                from rest_framework.routers import SimpleRouter,DefaultRouter
                router=SimpleRouter()
                router.register('publish',views.PublishView)
                、、、、
                url(r'', include(router.urls)),
            #DefaultRouter自动生成四条路由
                from rest_framework.routers import SimpleRouter,DefaultRouter
                router=DefaultRouter()
                router.register('publish',views.PublishView)
                、、、、
                url(r'', include(router.urls)),

 

 

Parser

(Generally do not need dynamic, most projects start global configuration click on it)
        role is to control my view class can parse the front passed over what kind of format is
        used globally:
            configuration setting in:
                REST_FRAMEWORK = {
                    "DEFAULT_PARSER_CLASSES": [
                        'rest_framework.parsers.JSONParser',
                    ]

                }
        global use:
            in the view class:
                parser_classes = [JSONParser,]
                
        - Source process:
            - to perform the analysis method when the calling request.data ---- "according to the coding pass over Options a parser object, call the parser object parser complete analytical method

 

 

Responder

        Import JSONRenderer rest_framework.renderers -from, BrowsableAPIRenderer
        - do not move, it can use the global configuration
        - Global Use:
            - arranged in the setting
                'DEFAULT_RENDERER_CLASSES': [XXX, XXX]
        - topical use:
            - arranged in the view class:
                renderer_classes = [JSONRenderer, BrowsableAPIRenderer]            

 

version control

        - for controlling the acting version
        - Global Use:
            - setting in the configuration:    
                'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.URLPathVersioning',
                'DEFAULT_VERSION': 'V1', # default version (not taken from the request object, which is displayed default)
                'ALLOWED_VERSIONS': [ 'V1', 'V2'], allowing version #
                'VERSION_PARAM': 'version' # URL acquired values Key
            - route to modify
                ? -url (r '^ (P <version > [V1 | V2] +) / Test / ', views.Test.as_view ()),
            - in the view class can be by: request.version taken which version currently accessed, corresponding to respective versions of the code executed taking

 

Guess you like

Origin www.cnblogs.com/tuanzibuku/p/11140515.html