亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Django REST framework

發布時間:2020-06-24 17:43:23 來源:網絡 閱讀:646 作者:大牙啊 欄目:編程語言


第1章?簡介

Django rest framework?是一套基于django框架編寫restful風格API的組件

第2章?簡單了解下都有哪些特點

2.1serializers(序列化)

就是將復雜的數據結構json化,支持ORM和非ORM數據源的序列化

2.2authentication(身份認證)

快速實現支持auth2auth3的方式的權限驗證

2.3request and response(請求與響應)

擴展了常規的HttpResquestTemplateResponse

2.4viewset and routers(視圖和路由)

第3章?開始一個簡單的項目

3.1創建好Django項目后,修改settings文件

INSTALLED_APPS= [
????
…….
????
'rest_framework',
????
'app',
]


DATABASES = {
????
'default': {
????????
'ENGINE':?'django.db.backends.mysql',
????????
'NAME':?'restful',
????????
'USER':?'root',
????????
'PASSWORD':?'123465',
????????
'HOST':?'local.dev',
????????
'PORT':?'3306',
????
}
}

?

3.2配置urls文件

from?django.conf .urls?import?url,?include


urlpatterns = [
????path(
'admin/',?admin.site.urls),
????
path(r'^test/',?include('app.urls')),
]

3.3編寫model層:

from?django.dbimport?models

# Create your models here.
class?Restful(models.Model):
????name= models.CharField(
max_length=30)
????passwd = models.CharField(
max_length=10)

????
def__str__(self):
????????
returnself.name

3.4初始化數據庫

python manage.py makemigrations

python manage.py migrate

3.5編寫序列化模塊:

from?rest_frameworkimport?serializers
from?.models?import?Restful

class?RestfulSerializer(serializers.ModelSerializer):
????
classMeta:
????????model = Restful
????????fields = (
"id",?"name",?"passwd")

3.6編寫視圖views.py

from?django.shortcutsimport?render

# Create your views here.
from?rest_framework?import?status,?generics
from?rest_framework.response?import?Response
from?rest_framework.views?import?APIView
from?.models?import?Restful
from?.serializer?import?RestfulSerializer


class?RestfulView(APIView):
????
defget(self,?request,?format=None):
????????data = Restful.objects.all()
????????serializer = RestfulSerializer(data
,?many=True)
????????
returnResponse(serializer.data)

????
defpost(self,?request,?format=None):
????????serializer = RestfulSerializer(
data=request.data)
????????
ifserializer.is_valid():
????????????serializer.save()
????????????
returnResponse(serializer.data,?status=status.HTTP_201_CREATED)
????????
else:
????????????
returnResponse(serializer.data,?status=status.HTTP_400_BAD_REQUEST)

3.7編寫app路由urls.py

from?django.conf.urlsimport?url
from?.?import?views

urlpatterns = [
????url(
r'^api/$',?views.RestfulView.as_view(),?name='restful'),
]

3.8進行測試:

插入數據:

???~ curl -X POST http://localhost:8000/test/api/ -d "name=jiangby&passwd=123456"

{"id":1,"name":"jiangby","passwd":"123456"}%????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???~ curl -X POST http://localhost:8000/test/api/ -d "name=test&passwd=123456"

{"id":2,"name":"test","passwd":"123456"}%

查看數據:

Django REST framework

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

泰顺县| 南汇区| 梁河县| 邵阳县| 那曲县| 舟曲县| 若尔盖县| 永兴县| 侯马市| 滦平县| 陇西县| 濮阳市| 池州市| 开江县| 广灵县| 庐江县| 巢湖市| 乐业县| 隆昌县| 澎湖县| 祁连县| 平安县| 西宁市| 渭源县| 布拖县| 资源县| 亳州市| 航空| 城市| 陇南市| 嘉祥县| 偏关县| 格尔木市| 蒙自县| 松溪县| 六安市| 广西| 金乡县| 琼中| 广宁县| 大荔县|