Django REST Framework tutorial (one minute entry)

  introduction

  Before using Django Rest Framework, we must first know what it is, can you use?

  Quoted the official saying: Django REST framework is a powerful and flexible tool for building Web API function package.

  So why use Rest Framework?

  Django REST Framework can be quickly implemented on the basis of Django's API, and also with their own WEB test page, you can easily test their API.

  Web application model

  Interface this thing when we django development projects, often come into contact, then django-web development interface is what? First look at the front and rear end web development model "and not separated before and after the django separation" , I posted an article on CSDN simple narrative django development model.

Overall it is:

web application mode in two ways:

             1. The front and rear ends are not separated

        Reference model is not separated from the rear end of the former, see the front page of the effect is controlled by the rear end, or redirect page rendered by the rear end, a rear end that is necessary to control the display of the front end, the front end and the rear end of the coupling is very high, this model more suitable for pure web application, but when the back-end docking APP, App may not require back-end returns an HTML page, and second, just the data itself, so the back-end web front-end interface to return to the original application APP is not applicable, In order to be re-docking APP Hi develop a back-end interface.

        You can see I wrote this before DJango blog you know, to return are using render or redirect each time you return, you need to put HTML pages and parameters

             2. The front end of the separation

          Separating the rear end of the previous application mode, the front end of the rear end of return data required, no rendering HTML pages, no control effect of the front end, the front end as long as the user to see what effect, requests the data from the rear end to the front end of how to load , the front end by its own decision, web page has its own approach, APP APP have handled, but no matter what front-end data required is basically the same, only to develop a back-end logic can provide external data, in separate front and rear ends of the application mode, the front and rear ends of the degree of coupling is relatively low

          Separating the rear end of the previous application mode, we typically developed for each view to become a back-end interface, or the API, to the front end of the CRUD data access interface

If the needs of the project is the latter, then you can use the Django Rest Framework.

Interested can refer to the official information: https://www.django-rest-framework.org/ , if the partner can not read English, can refer to the rest of the Chinese document: http://www.iamnancy.top/djangorestframework/ Home /

  Django REST Framework Project

1. Open pycharm editor, new projects, selected as follows:

 Here virtual environment is django_rest, if not read, you can refer to what I wrote earlier virtual environments article.

2. After the project structure built as follows:

 Install Django REST framework in a virtual environment created:

  Development steps

  Environment and above already installed package, start now developed.

1. First, the configuration setting file, add rest_framework application.

2. Global Settings, paging, and API usage rights issue, specify administrator:

# Set permission policy: 
REST_FRAMEWORK = { 
    'DEFAULT_PERMISSION_CLASSES': [ 
        'rest_framework.permissions.IsAdminUser', 
    ], 
    'PAGE_SIZE': 10 
}

3. Data Migration

4. Create an administrator account:

5. Create data serialization, mainly to display data:

New serializers.py api file in the following applications

Code:

from django.contrib.auth.models import User, Group
from rest_framework import serializers


class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'groups')


class GroupSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Group
        fields = ('url', 'name')

 Description: serializers form of a user-defined API, which fields such return, what format, where the sequence of django own User and Group.

6. Create a view function:

Import the User django.contrib.auth.models from, Group 
from rest_framework Import viewsets 
from tutorial.quickstart.serializers Import UserSerializer, GroupSerializer 


class UserViewSet (viewsets.ModelViewSet): 
    "" " 
    . allows the user to view or edit the path API " 
    "" 
    QuerySet User.objects.all = () ORDER_BY ( '- date_joined'). 
    serializer_class = UserSerializer 


class GroupViewSet (viewsets.ModelViewSet): 
    "" " 
    . allow API group view or edit the path " 
    "" 
    QuerySet Group.objects.all = ( ) 
    serializer_class = GroupSerializer

 

7. django_restAPI directory url.py added api routing configurations:

Import django.conf.urls URL from, the include 
from rest_framework Import Routers 
from tutorial.quickstart Import views 

Router routers.DefaultRouter = () 
router.register (r'users ', views.UserViewSet) 
router.register (r'groups', views .GroupViewSet) 

# use the automatic URL routing connection to our API. 
# In addition, we also include support for browser to the login URL API. 
= the urlpatterns [ 
    URL (R & lt '^', the include (router.urls)), 
    URL (R & lt 'API-the auth ^ /', the include ( 'rest_framework.urls', namespace = 'rest_framework')) 
]

 

8. All this stuff has been finished, and then we run the runserver command:

 

 403 interface has returned, suggesting no authority, we landed about to try:

 

 Two return url, add a user group, a user is added. Some people may have doubts, why two? Because when the front serialized data, the data show two forms of writing, we have a comment, and then try to run:

 

View interface:

 

Click on the link to add users, add users

 

 

 

 

 

This is through the interface in the form of user groups and users show up, we can see the sign in the background:

 So, rest is presented to us API, in order to adjust the interface to operate in the form of a database, admin and management background in the form of operating and display data pages.

Observant people can be found, why status is a red cross, because the sequence of the time did not show the status field, and now I will put it out:

And then run the program, after adding users to view:

 

 

 View into the back-end management page:

 It is not open, and if the students do not worry, you can go look at the database:

 

This, django_rest_framework introductory tutorial has finished, regardless of whether the future development API, whether used for learning django novice to learn about design patterns and is also a good idea.

  If you are interested in related technology partner python test development, test development welcome to join the study and exchange QQ group: 696 400 122, a short step, a thousand miles.



Guess you like

Origin www.cnblogs.com/liudinglong/p/12549162.html