您好,登錄后才能下訂單哦!
在項目的setting.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', 'rest_framework', 'app01', )
其中,app01是我創(chuàng)建的一個app
3.在項目的urls.py文件中加入
from django.conf.urls import url, include from django.contrib.auth.models import User from rest_framework import routers, serializers, viewsets #serilizers 數(shù)據(jù)訪問層:定義API的表現(xiàn)形式;將models變?yōu)榭尚蛄谢模┢渌到y(tǒng)調(diào)用; #viewsets 邏輯處理層:定義展示行為 #routers 和django的url路由系統(tǒng)差不多,自動決定url 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)), ]
這樣就可以在首頁匹配到rest-framework的api了
4.自己定義一個blog的api
在app01的models.py文件中創(chuàng)建Blog類:
from django.db import models # Create your models here. class Blog(models.Model): title = models.CharField(max_length = 50 ) content = models.TextField()
很簡單的一個blog類,只有title和content 2個字段
app01下的api.py文件:
from django.contrib.auth.models import User from rest_framework import routers, serializers, viewsets from app01 import models from rest_framework import response 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 BlogSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = models.Blog depth = 1 fields = ('url','title','content',) class BlogViewSet(viewsets.ModelViewSet): queryset = models.Blog.objects.all() serializer_class = BlogSerializer
api.py文件使用了rest-framework將User類和Blog類提供了api接口
修改urls.py文件,讓user和blog2個資源可以api調(diào)用:
from django.conf.urls import include, url from django.contrib import admin from rest_framework import routers from app01 import api from app01 import views router = routers.DefaultRouter() router.register(r'users', api.UserViewSet) router.register(r'blogs', api.BlogViewSet) urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^',include(router.urls)), ]
5.一開始blog為空,先開啟django的后臺管理
在app01下的models.py文件中加入:
from django.contrib import admin # Register your models here. from .models import Blog admin.site.register(Blog)
這樣就可以用后臺管理Blog了,當(dāng)然,還得創(chuàng)建一個管理員賬號密碼,在項目的目錄下執(zhí)行:
python manage.py createsuperuser
輸入要創(chuàng)建的賬號密碼即可
6.啟動django程序,輸入
http://127.0.0.1:8000/admin
直接到后臺管理blog
沒有數(shù)據(jù),然后添加數(shù)據(jù)
添加了2條記錄
上面是數(shù)據(jù)庫的blog表
7.使用api查看blog數(shù)據(jù)
由于在urls.py中
url(r'^',include(router.urls)),
這一句直接讓rest framework處理所有的url,所以首頁就可以看到我們在api.py中定義的user和
直接點鏈接blogs的,
在django中我們沒有創(chuàng)建對數(shù)據(jù)庫的查詢操作和頁面返回,但是調(diào)用rest framework標(biāo)準(zhǔn)的api接口我們就可以直接從數(shù)據(jù)庫中查詢到數(shù)據(jù),增刪改查都是可以的!實現(xiàn)起來如此簡單!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。