pip install djangorestframework
INSTALLED_APPS = ( ... 'rest_framework', ) REST_FRAMEWORK = { # 編碼格式 'UNICODE_JSON': False, }
urlpatterns = [ ... url(r'^api/', include('web.rest_urls')), ]
from django.conf.urls import url, include from rest_framework import routers from web.rest_views import * from web.views import account_list, account_detail # Routers provide an easy way of automatically determining the URL conf. # 註冊 router = routers.DefaultRouter() router.register(r'account', AccountViewSet) # Wire up our API using automatic URL routing. # Additionally, we include login URLs for the browsable API. urlpatterns = [ # 默認 url(r'^', include(router.urls)), # 自定義功能 url(r'^account_list/$', account_list), url(r'^account_detail/(\d+)/$', account_detail), # api認證 url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) ]
#!/usr/bin/env python from rest_framework import viewsets from web.rest_searilizers import AccountSerializer from web import models # ViewSets define the view behavior. class AccountViewSet(viewsets.ModelViewSet): queryset = models.Account.objects.all() serializer_class = AccountSerializer
#!/usr/bin/env python from rest_framework import serializers from web import models # Serializers define the API representation. class AccountSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = models.Account #關鍵字段 fields = ('email', 'name', 'role', 'customer')
views 自定義功能html
# rest from rest_framework.decorators import api_view from web import rest_searilizers from rest_framework import status from rest_framework.response import Response @api_view(['GET', 'POST']) def account_list(request): """ List all snippets, or create a new snippet. """ if request.method == 'GET': account_obj = models.Account.objects.all() serializer = rest_searilizers.AccountSerializer(account_obj, many=True) return Response(serializer.data) elif request.method == 'POST': print("request", request.data) serializer = rest_searilizers.AccountSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) @api_view(['GET', 'PUT', 'DELETE']) @csrf_exempt def account_detail(request, pk): """ Retrieve, update or delete a snippet instance. """ # pk 跟 id 是同樣的 try: account_obj = models.Account.objects.get(pk=pk) except account_obj.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = rest_searilizers.AccountSerializer(account_obj) return Response(serializer.data) elif request.method == 'PUT': serializer = rest_searilizers.AccountSerializer(account_obj, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': account_obj.delete() return Response(status=status.HTTP_204_NO_CONTENT)
訪問 http://127.0.0.1:8000/api/account_list/?format=json ,能夠查看到接口提供的數據python
詳細參考 http://www.django-rest-framework.org/#exampleweb
restful-api 設計規範 http://www.cnblogs.com/alex3714/articles/6808013.htmldjango