Django rest framework1

Source: Internet
Author: User

Content review:

1. development Mode-general development mode (with the frontend and backend written together)-frontend and backend separation 2. backend development provides a URL for the front end (API/interface development). Note: httpresponse3.django fbv, CBV fbv, function base view def users (request): user_list = ['Alex ', 'oldboys'] Return httpresponse (JSON. dumps (user_list) CBV, class base view routing: URL (R' ^ students/', views. studentsview. as_view (), view: from Django. views import View class studentsview (View): def get (self, request, * ARGs, ** kwargs): Return httpresponse ('get') def post (self, request, * ARGs, ** kwargs): Return httpresponse ('post') def put (self, request, * ARGs, ** kwargs): Return httpresponse ('put ') def Delete (self, request, * ARGs, ** kwargs): Return httpresponse ('delete') 4. list generative class FOO: pass class bar: Pass v = [item () for item in [Foo, bar] V Object List 5. object-oriented-encapsulation-encapsulate the same class method in the class file: file addition, deletion, modification, query method class DB: database method-encapsulate data in the object class file: def _ init _ (self, a1, a2): Self. a1 = A1 self. xxx = a2 def get :... obj1 = file (123,456)

Summary today:

1. Restful specification (recommended) 2. Django rest Framework framework

Details:

0. fbv and CBV. Different methods are implemented based on reflection based on different request methods. Principle:. route URL-> View method-> dispatch method (Other reflection execution: Get/post/delete/Put) interview question: 1. django middleware-process_request-process_view-process_response-process_exception-process_render_template 2. what have you done with middleware? -Permission,-User Logon verification-how does Django's csrf implement? Process_view method-check whether the view is @ csrf_exempt (exempt from csrf authentication)-Get token from the request or cookie 3. middleware = ['django. middleware. security. securitymiddleware ', 'django. contrib. sessions. middleware. sessionmiddleware ', 'django. middleware. common. commonmiddleware ', 'django. middleware. csrf. csrfviewmiddleware ', # Use csrf to authenticate 'django. contrib. auth. middleware. authenticationmiddleware ', 'django. contrib. messages. middleware. messagemiddleware ', 'django. middleware. clickjacking. xframeoptionsmiddleware ',] from Django. views. decorators. csrf import csrf_exempt @ csrf_exempt # This function does not require authentication def users (request): user_list = ['Alex ', 'oldboy'] Return httpresponse (JSON. dumps (user_list) CBV tips:-method_decorator (csrf_exempt)-In the dispatch method (the Independent method is invalid) from Django. views. decorators. csrf import csrf_exempt from Django. utils. decorators import method_decorator @ csrf_exempt def users (request): user_list = ['Alex ', 'oldboys'] Return httpresponse (JSON. dumps (user_list) from Django. views import View class studentsview (View): @ method_decorator (csrf_exempt) def dispatch (self, request, * ARGs, ** kwargs): return Super (studentsview, self ). dispatch (request, * ARGs, ** kwargs) def get (self, request, * ARGs, ** kwargs): Return httpresponse ('get') def post (self, request, * ARGs, ** kwargs): Return httpresponse ('post') def put (self, request, * ARGs, ** kwargs): Return httpresponse ('put ') def Delete (self, request, * ARGs, ** kwargs): Return httpresponse ('delete') Summary:-essence: Reflection-based implementation-process: routing, view, dispatch (reflection)-cancel csrf authentication (decorator must be added to the dispatch method and method_decorator decoration) Extension:-csrf-middleware-based process_viewfang1.restful specification (recommended). urlpatterns = [# URL (R' ^ admin/', admin. site. URLs), URL (R' ^ get_order/', views. get_order), URL (R' ^ add_order/', views. add_order), URL (R' ^ del_order/', views. del_order), URL (R' ^ update_order/', views. update_order)] def get_order (request): Return httpresponse ('') def add_order (request): Return httpresponse ('') def del_order (request): Return httpresponse ('') def update_order (request): Return httpresponse ('') B. restful specification (recommended) 1. perform different operations according to the method. The example is based on fbv: URL (r '^ order/', views. order) def Order (request): If request. method = 'get': Return httpresponse ('get Order') Elif request. method = 'post': Return httpresponse ('create Order') Elif request. method = 'put': Return httpresponse ('Update Order') Elif request. method = 'delete': Return httpresponse ('delete Order') based on CBV: urlpatterns = [url (R' ^ order/', views. orderview. as_view ()] class orderview (View): def get (self, request, * ARGs, ** kwargs): Return httpresponse ('get Order') def post (self, request, * ARGs, ** kwargs): Return httpresponse ('create Order') def put (self, request, * ARGs, ** kwargs ): return httpresponse ('Update Order') def Delete (self, request, * ARGs, ** kwargs): Return httpresponse ('delete Order') C. let's talk about our understanding of restful API specifications. 1. essentially a Specification 2. some operations on the API are reflected in the URL. 2. django rest Framework framework pip3 install djangorestframework. authentication-use only-source code process dispatch...

Django rest framework1

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.