Model: 데이터 구조화
View: 데이터가 흘러다니는 곳
Template: 데이터를 표시하는 곳
- Application Programming Interface
- 개발자용 접점
- 개발자는 Data만 필요함!
- 개발자용 접점
: 요청은 URL로 보낸다!
: 약속
- JSON
- JavaScript Object Notation
- Javascript 객체식 표기법
- XML
- eXtended Markup Language (W3C, 1996)
: key값이 반영이 안 됨!
- 그래서 등장한 것이
tag
를 내맘대로 정의할 수 있는 XML
- XML이 닫는 tag 때문에 길이가 길다
- 돈
: Django 에서 JSON 형식에 맞춰서 Data만 제공한다!
-
좋은 유저 경험을 위해서
-
UX 안 좋으면 -> User X -> 돈 X
-
data -> 인간이 뭘 좋아할까?
-
모바일 어플리케이션 (웹)
-
churn (이탈율)
-
JS 필수 (Adobe Flash)
-
-
분리되어 있는 것이 편해서
pip uninstall django
pip install django==2.1.15
pip install faker
In [1]: from faker import Faker
In [2]: f = Faker()
In [3]: f.text()
Out[3]: 'Soldier live various argue many expect important once. Next possible whom I.\nSome national left wall score few else always. Action less culture spring any night.'
In [4]: f.name()
Out[4]: 'Jenna Davis'
In [5]: f.paragraph()
Out[5]: 'Improve knowledge hot matter himself. Growth water act bill to can discuss there. Follow out person vote action someone.'
In [6]: f.paragraph(4)
Out[6]: 'Early program four bill. Comput
: url을 깔끔하게 정리하는 방식 (공통의 rule / 약속)
- HTTP verb (GET, POST)
- 명사 (복수형)로 구정
- 동사 URL에 집어 넣지마! ->
HTTP method
활용해- C (POST)
(POST) / articles /
- R (GET)
- index (모든 정보) -
(GET) / articles /
- detail (하나의 정보) -
(GET) / articles / <id>
- index (모든 정보) -
- U (PUT/PATCH)
(PUT) / articles / <id>
- D (DELETE)
(DELETE) / articles / <id>
- C (POST)
- 목적어만 URL에 집어 넣어 -> 복수형으로
- Data
-
subdomain
- ex)
- lab.ssafy.com
- api.gitbub.com
- ex)
-
분리 URL /api/
- ssafy.com/api/lectures/
- github.com/api/repos/
-
versionning
- ssafy.com/api/v1/lectures/
- POST /api/articles/1/like/
- POST /api/articles/1/comments/like/
djangorestframework 설치
pip install djangorestframework
설치되어 있는지 확인
$ pip show djangorestframework
Name: djangorestframework
Version: 3.11.0
Summary: Web APIs for Django, made easy.
Home-page: https://www.django-rest-framework.org/
Author: Tom Christie
Author-email: [email protected]
License: BSD
Location: /home/chloe/.local/lib/python3.6/site-packages
Requires: django
Required-by: drf-serializer-cache
포맷의 변환 (데이터를 전송/이동)
dict -> JSON (stringify, serialize
)
JSON -> dict (parse, deserialize
)
ex)
@api_view(['POST'])
def article_create(request):
# 글을 생성
serializer = ArticleSerializer(data=request.data)
if serializer.is_valid(raise_exception=True):
serializer.save()
return Response(serializer.data)
하나만 보냄
에러메시지
- API 관련 문서를 자동으로 생성
pip install drf-yasg
├── api
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── db.sqlite3
├── manage.py
└── musics
├── admin.py
├── apps.py
├── fixtures
│ └── dummy.json
├── __init__.py
├── migrations
├── models.py
├── serializers.py
├── tests.py
├── urls.py
└── views.py
7 directories, 29 files
$ python manage.py loaddata dummy.json
Installed 14 object(s) from 1 fixture(s)
$ python manage.py dumpdata musics
[{"model": "musics.artist", "pk": 1, "fields": {"name": "Coldplay"}}, {"model": "musics.artist", "pk": 2, "fields": {"name": "Maroon5"}}, {"model": "musics.music", "pk": 1, "fields": {"artist": 2, "title": "Girls Like You"}}, {"model": "musics.music", "pk": 2, "fields": {"artist": 2, "title": "Sunday Morning"}}, {"model": "musics.music", "pk": 3, "fields": {"artist": 1, "title": "viva la vida"}}, {"model": "musics.music", "pk": 4, "fields": {"artist": 1, "title": "paradise"}}, {"model": "musics.comment", "pk": 1, "fields": {"music": 1, "content": "\uac78\uc2a4 \ub77c\uc78c \uc720!!!"}}, {"model": "musics.comment", "pk": 2, "fields": {"music": 1, "content": "\ub9c8\ub8ec \ud30c\uc774\ube0c \uc9f1\uc9f1!"}}, {"model": "musics.comment", "pk": 3, "fields": {"music": 2, "content": "\uc77c\uc694\uc77c \ubaa8\ub2dd~~~"}}, {"model": "musics.comment", "pk": 4, "fields": {"music": 2, "content": "\ud558\uc9c0\ub9cc \ub0b4\uc77c\uc740 \uc6d4\uc694\uc77c"}}, {"model": "musics.comment", "pk": 5, "fields": {"music": 3, "content": "10\ub144\uc774 \uc9c0\ub098\ub3c4 \uc88b\uc544"}}, {"model": "musics.comment", "pk": 6, "fields": {"music": 3, "content": "\ub9c8\uce58 \ub0b4\uac00 \uc655\uc774 \ub41c \uac83 \uac19\uc544!"}}, {"model": "musics.comment", "pk": 7, "fields": {"music": 4, "content": "\ud30c\ub77c\ub2e4\uc774\uc2a4 \ud30c\ub77c\ud30c\ub77c\ud30c\ub77c\ub2e4\uc774\uc2a4~"}}, {"model": "musics.comment", "pk": 8, "fields": {"music": 4, "content": "\uc228\uaca8\uc9c4 \uba85\uace1!!!"}}]
이렇게 하면 다닥다닥 붙어있음
python manage.py dumpdata musics > dump.json
--indent 2
-> indenting을 2 줘라
python manage.py dumpdata musics --indent 2 > dump2.json
result
[
{
"model": "musics.artist",
"pk": 1,
"fields": {
"name": "Coldplay"
}
},
{
"model": "musics.artist",
"pk": 2,
"fields": {
"name": "Maroon5"
}
},
{
"model": "musics.music",
"pk": 1,
"fields": {
"artist": 2,
"title": "Girls Like You"
}
},
{
"model": "musics.music",
"pk": 2,
"fields": {
"artist": 2,
"title": "Sunday Morning"
}
},
{
"model": "musics.music",
"pk": 3,
"fields": {
"artist": 1,
"title": "viva la vida"
}
},
{
"model": "musics.music",
"pk": 4,
"fields": {
"artist": 1,
"title": "paradise"
}
},
{
"model": "musics.comment",
"pk": 1,
"fields": {
"music": 1,
"content": "\uac78\uc2a4 \ub77c\uc78c \uc720!!!"
}
},
{
"model": "musics.comment",
"pk": 2,
"fields": {
"music": 1,
"content": "\ub9c8\ub8ec \ud30c\uc774\ube0c \uc9f1\uc9f1!"
}
},
{
"model": "musics.comment",
"pk": 3,
"fields": {
"music": 2,
"content": "\uc77c\uc694\uc77c \ubaa8\ub2dd~~~"
}
},
{
"model": "musics.comment",
"pk": 4,
"fields": {
"music": 2,
"content": "\ud558\uc9c0\ub9cc \ub0b4\uc77c\uc740 \uc6d4\uc694\uc77c"
}
},
{
"model": "musics.comment",
"pk": 5,
"fields": {
"music": 3,
"content": "10\ub144\uc774 \uc9c0\ub098\ub3c4 \uc88b\uc544"
}
},
{
"model": "musics.comment",
"pk": 6,
"fields": {
"music": 3,
"content": "\ub9c8\uce58 \ub0b4\uac00 \uc655\uc774 \ub41c \uac83 \uac19\uc544!"
}
},
{
"model": "musics.comment",
"pk": 7,
"fields": {
"music": 4,
"content": "\ud30c\ub77c\ub2e4\uc774\uc2a4 \ud30c\ub77c\ud30c\ub77c\ud30c\ub77c\ub2e4\uc774\uc2a4~"
}
},
{
"model": "musics.comment",
"pk": 8,
"fields": {
"music": 4,
"content": "\uc228\uaca8\uc9c4 \uba85\uace1!!!"
}
}
]