您好,登錄后才能下訂單哦!
Python Version: 2.7.10
Django Version: 1.9
REST framework 3.3.2
安裝
pip install django
pip install djangorestframework
settings.py INSTALLED_APPS中引入rest_framework
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app01', 'rest_framework', ]
創(chuàng)建model, 修改mdels.py
class Book(models.Model): name=models.CharField(max_length=100) title=models.CharField(max_length=100) author=models.CharField(max_length=100)
創(chuàng)建serializers.py
from app01.models import * from rest_framework import serializers class BookSerializer(serializers.Serializer): name = serializers.CharField(max_length=100) title = serializers.CharField(max_length=100) author = serializers.CharField(max_length=100) def create(self, validated_data): return Book(**validated_data) def update(self, instance, validated_data): instance.email = validated_data.get('name', instance.name) instance.content = validated_data.get('title', instance.title) instance.created = validated_data.get('author', instance.author) return instance
restore_object() 3.0以上版本不支持已經(jīng)被create()和update()方法代替,源碼如下:
( 'Serializer `%s.%s` has old-style version 2 `.restore_object()` ' 'that is no longer compatible with REST framework 3. ' 'Use the new-style `.create()` and `.update()` methods instead.' % (self.__class__.__module__, self.__class__.__name__) )
創(chuàng)建基于類和函數(shù)的視圖
from django.shortcuts import render_to_response from django.http import HttpResponse from app01.forms import * from rest_framework.views import APIView from app01.serializers import BookSerializer from app01.models import * from rest_framework.response import Response from rest_framework import generics from rest_framework import status # Create your views here. class BookList(APIView): def get(self,request,format=None): books=Book.objects.all() ser=BookSerializer(books, many=True) return Response(ser.data) def post(self,request,format=None): ser=BookSerializer(data = request.data) if ser.is_valid(): ser.create(ser.validated_data).save() return Response(ser.data,status = status.HTTP_201_CREATED) return Response(ser.errors) class BookDetail(APIView): def get(self,request,num,format=None): b=Book.objects.get(id=num) ser=BookSerializer(b) return Response(ser.data) class GenericBookList(generics.ListCreateAPIView): queryset = Book.objects.all() serializer_class = BookSerializer def post(self,request,format=None): ser=BookSerializer(data = request.data) if ser.is_valid(): ser.create(ser.validated_data).save() return Response(ser.data,status = status.HTTP_201_CREATED) return Response(ser.errors) def index(request): return render_to_response('index.html') def hello(request): if request.method == 'POST': form = BookForms(request.POST) if form.is_valid(): data = form.cleaned_data title = data["title"] return HttpResponse(title) form = BookForms() return render_to_response('1.html', {'form':form})
urls.py配置
from django.conf.urls import url from django.contrib import admin from app01.views import * urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^$', index), url(r'^hello/$', hello), url(r'^book/$', BookList.as_view()), url(r'^genericbook/$', GenericBookList.as_view()), url(r'^book/(\d+)', BookDetail.as_view()), ]
APIView
generic通用視圖
RESTful Request:GET/PUT/DELETE/POST/HEAD/OPTIONS
POST請(qǐng)求被定義為創(chuàng)建“從屬資源”(擁有父資源的資源) (add)
參考文章:
http://www.lifelaf.com/blog/?p=156
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。