Response JSON
๊ฐ์
โ ์๋ฒ๋ ํ์ด์ง(html)๋ฟ๋ง ์๋๋ผ ๋ค์ํ ๋ฐ์ดํฐ ํ์
์ ์๋ตํ ์ ์๋ค.
โ JSON ๋ฐ์ดํฐ๋ฅผ ์๋ตํ๋ ์๋ฒ๋ก์ ๋ณํ
โ ํ๋ฉด์ ๋ค๋ฅธ Front-end Framework๊ฐ ๊ตฌ์ฑ
์ฌ์ ์ค๋น
- migrate ์งํ (์ฌ์ง ์๋ต)
- ์ค๋น๋ ํ์ผ loadํด์ ์ด๊ธฐ ๋ฐ์ดํฐ ์
๋ ฅ
- url ์ดํด๋ณด๊ธฐ
Response
- HTML ์๋ต
JsonResponse()
์ ์ฌ์ฉํ JSON ์๋ต- Django Serializer๋ฅผ ์ฌ์ฉํ JSON ์๋ต
- Djano REST framework๋ฅผ ์ฌ์ฉํ JSON ์๋ต
1. HTML ์๋ต
![image](https://user-images.githubusercontent.com/109324637/196100540-46514975-aab8-454d-964a-e0416ef1cd14.png)
![image](https://user-images.githubusercontent.com/109324637/196100387-033a72f3-12cc-43fe-bdb4-38d17e9212fe.png)
![image](https://user-images.githubusercontent.com/109324637/196100747-03d4acc7-ac9b-4590-a0e0-b166f3ca05b2.png)
โ ๊ธฐ์กด์ ํ๋ ๋ฐฉ์
2. JsonResponse()
๋ฅผ ์ฌ์ฉํ JSON ์๋ต
![image](https://user-images.githubusercontent.com/109324637/196101232-8b8e9ab5-639b-4f1e-8a19-64c23518ecb3.png)
![image](https://user-images.githubusercontent.com/109324637/196101356-f15f53cf-65d7-4ca7-b708-0e1261a269d7.png)
![image](https://user-images.githubusercontent.com/109324637/196101424-1c9fd8d7-9240-4ea4-bb28-432b6c2d8821.png)
โ JSON ํํ๋ก ๋ฐํํ๋ค.
3. Django Serializer๋ฅผ ์ฌ์ฉํ JSON ์๋ต
![image](https://user-images.githubusercontent.com/109324637/196101823-e7bb28f8-b724-4894-a1b2-f0d6193ef28c.png)
![image](https://user-images.githubusercontent.com/109324637/196102092-7d36ec3e-7f5b-467b-ae2a-b2bfe77c4fc1.png)
โ ๋ชจ๋ธ ๊ตฌ์กฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก djang๊ฐ ์์์ JSON ํํ๋ก ๋ฐํํ๋ค.
Serialization
- ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ ๊ฐ์ฒด ์ํ๋ฅผ ๋์ผ ํน์ ๋ค๋ฅธ ์ปดํจํฐ ํ๊ฒฝ์ ์ ์ฅํ๊ณ , ๋์ค์ ์ฌ๊ตฌ์ฑํ ์ ์๋ ํฌ๋งท์ผ๋ก ๋ณํํ๋ ๊ณผ์
- "๋์ค์ ๋ค์ ์ฝ๊ฒ ์ฌ์ฉํ ์ ์๋ ํฌ๋งท์ผ๋ก ๋ณํํ๋ ๊ณผ์ "
- Django์
serialize()
๋ Queryset ๋ฐ Model Instance๋ฅผ JSON ๋ฑ์ ์ ํ์ผ๋ก ์ฝ๊ฒ ๋ณํํ ์ ์๋ Python ๋ฐ์ดํฐ ํ์ ์ผ๋ก ๋ง๋ค์ด ์ค๋ค.
4. Django REST framework๋ฅผ ์ฌ์ฉํ JSON ์๋ต
โ Django REST framework
- Django์์ Restfrul API ์๋ฒ๋ฅผ ์ฝ๊ฒ ๊ตฌ์ถํ ์ ์๋๋ก ๋์์ฃผ๋ ์คํ์์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ
![image](https://user-images.githubusercontent.com/109324637/196103167-ea0b74ad-4db4-4691-9dbc-afd71e2868f8.png)
โ DRF ์ค์น
![image](https://user-images.githubusercontent.com/109324637/196103366-f74e04d4-602d-4acf-a72f-ccaf60f1f769.png)
โ ModelForm๊ณผ ๊ตฌ์กฐ๊ฐ ์ ์ฌํ๋ค
![image](https://user-images.githubusercontent.com/109324637/196103659-a9552465-8e78-439f-aeb8-45ac6a915d2f.png)
![image](https://user-images.githubusercontent.com/109324637/196103751-dab2e187-5524-458f-ac26-55062bba6705.png)
โ DRF ํ
ํ๋ฆฟ ๊ธฐ๋ฐ์ผ๋ก JSON ํ์ผ์ด ์ถ๋ ฅ๋๋ ๋ฌธ์๋ฅผ ์ ๊ณตํด์ค๋ค.
![image](https://user-images.githubusercontent.com/109324637/196103949-f6af0dd3-7bc5-4cb1-aeb7-dd41ad212300.png)
โ ๋ธ๋ผ์ฐ์ ์์๋ JSON์ ๋ฌธ์(Text)๋ก ์ถ๋ ฅํด์ฃผ์ง๋ง ์ค์ ์ฝ๋์์ผ๋ก JSON์ด ์ ๊ณต๋๋ค.
request ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ด์ฉํ์ฌ JSON ์๋ต ๋ฐ๊ธฐ
![image](https://user-images.githubusercontent.com/109324637/196104345-77823a1b-2448-4d1d-9dc9-f3f487734441.png)
![image](https://user-images.githubusercontent.com/109324637/196104566-d10f6d5f-9851-4722-bedc-92505c470ff8.png)
โ ์์ ๊ฐ์ด requests
๋ฅผ ์ด์ฉํ์ฌ JSON์ผ๋ก๋ถํฐ ํ์ํ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ ์ ์๋ค.
'โญ Personal_Study > Django' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Django REST framework - N:1 Relation (1) | 2022.10.27 |
---|---|
Django REST framework - Single Model (0) | 2022.10.26 |
(M:N) User- User Relationship (0) | 2022.10.18 |
(M:N) Article - User Relationship (0) | 2022.10.17 |
Django์์ css, js ๋ฑ static(์ ์ ) ์์ ๋ฑ๋ก/๊ด๋ฆฌํ๊ธฐ (0) | 2022.10.12 |
๋๊ธ