RESTFul is an API specification design is standardized interface for data transmission in the rear end of the front separating web development, so that the front and rear ends of uniform and efficient interaction.
RESTful core idea is that the client data manipulation instruction is issued "Verb + object" structure. For example, GET /articles
this command GET
is a verb /articles
is the object (the plural form). Usually if single operand data specified operation id (pk).GET /articles/?pk=1
The method of HTTP verb is usually five kinds corresponding CRUD operations (direct application manner corresponding request)
- GET: read (Read)
- POST: New (Create)
- PUT:更新(Update)
- PATCH: Update (Update), usually a partial update
- DELETE: 删除 (Delete)
For GET and POST only send clients can use X-HTTP-Method-Override: PUT, specify the request method
Django used RESTFul
Installation Module: pip install djangorestframework
Configuration and use:
settings中:INSTALL_APPS = [… , ’rest_framework’,]
The core djangorestframework is serialized:
Serializers.py file is created in the app, the need to write a sequence of models and patterns inside.
. 1 2 from rest_framework Import serializers . 3 from app01.models Import * . 4 . 5 # define the sequence of procedures . 6 class UserInfoSerializer (serializers.ModelSerializer): . 7 '' 'specify the required sequence of fields and model' '' . 8 class Meta -: . 9 Model database table name = Book # 10 fields = '__all__' # All fields are required
views.py application class and inherits view rest_framework package
. 1 from rest_framework.viewsets Import ModelViewSet 2 from app01.serializers Import * . 3 . 4 class UserInfoView (ModelViewSet): . 5 QuerySet Book.objects.all = () # acquires a query result set . 6 serializer_class = UserInfoSerializer specified serializer # 7
urls.py rest_framework routing can be specified in the route set DefaultRouter
. 1 from rest_framework.routers Import defaultrouter 2 # view of a process defined in the router . 3 Router defaultrouter = () . 4 Router. Register ( 'Users', views.UserInfoView, base_name = '') View Set # register in the router . 5 . 6 the urlpatterns = [ . 7 . 8 ] . 9 10 the urlpatterns router.urls + = . 11 12 is ------------------------------------- ---------------------------- 13 # primary routing 14 path ( 'API /', the include ((router.urls, 'APP_NAME '), namespace =' instance_name ' )),