Django rest framwork

Restful API

  • REST與技術無關,表明的是一種軟件架構風格,REST是Representational State Transfer的簡稱,中文翻譯爲「表徵狀態轉移」
  • REST從資源的角度類審視整個網絡,它將分佈在網絡中某個節點的資源經過URL進行標識,客戶端應用經過URL來獲取資源的表徵,得到這些表徵導致這些應用轉變狀態
  • REST與技術無關,表明的是一種軟件架構風格,REST是Representational State Transfer的簡稱,中文翻譯爲「表徵狀態轉移」
  • 全部的數據,不過是經過網絡獲取的仍是操做(增刪改查)的數據,都是資源,將一切數據視爲資源是REST區別與其餘架構風格的最本質屬性
  • 對於REST這種面向資源的架構風格,有人提出一種全新的結構理念,即:面向資源架構(ROA:Resource Oriented Architecture)

理解RESTful架構 : http://www.ruanyifeng.com/blog/2011/09/restful html

RESTful API 設計指南 : http://www.ruanyifeng.com/blog/2014/05/restful_api.html python

django中可使用 Django rest framwork 來實現:http://www.django-rest-framework.org/web

 

1、安裝:django

pip install djangorestframework
pip install markdown
pip install django-filter

   

2、配置
一、註冊settings 
       
'rest_framework'

  

二、配置URL   
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
   
三、引入模塊
from django.contrib.auth.models import User
from rest_framework import routers, serializers, viewsets
class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'is_staff')

class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

router = routers.DefaultRouter()
router.register(r'users', UserViewSet)

urlpatterns = [
    url(r'^', include(router.urls)),

 
以上用的是Django的User表,如今增長一個自定義Test的表
model.py
class Test(models.Model):
    UserName = models.CharField(max_length=50)
    PassWord = models.CharField(max_length=50)  
urls.py
from django.contrib.auth.models import User
from rest_framework import routers, serializers, viewsets
from web_api import models
class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'is_staff')
class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer
class TestSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = models.Test
        fields = ('UserName', 'PassWord')
class TestViewSet(viewsets.ModelViewSet):
    queryset = models.Test.objects.all()
    serializer_class = TestSerializer

router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
router.register(r'tests', TestViewSet)

urlpatterns = [
    url(r'^', include(router.urls)),

 
如今對錶進行增刪改查
 
GET 查: 默認就是GET
    
POST 增:

 
PUT 改: 增長url 字段 ,顯示每條數據對應的URL
class TestSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = models.Test
fields = ('url','UserName', 'PassWord')

 
 
 DELETE 刪

 
           
 
 
自定義api操做
 
from rest_framework.decorators import api_view
from rest_framework.response import Response
@api_view(['GET','PUT','POST'])     #定義只能容許接收的請求,屏蔽DELETE
def servers(request):
    method = request.method
    if method == 'POST':
        pass
    elif method == 'PUT':
        pass
    return Response('ok')         #用rest提供的Response,返回更友好的界面
 
urlpatterns = [
    url(r'servers/',views.servers),
    url(r'^', include(router.urls)),

  

這樣就能夠在頁面進行制定的操做,GET,PUT,POST,不能DELETE

 
查看原始JSON的返回值
http://127.0.0.1:8000/api/servers/?format=json
相關文章
相關標籤/搜索