참고) pip: python library 관리자 (npm 이랑 비슷)
# 프로젝트 폴더 생성
mkdir night
cd night
# 가상 환경 세팅
python3 -m venv djangovenv
# 가상 환경 접속 (djangovenv 에 들어가지말고)
source djangovenv/bin/activate
# 장고 설치
python3 -m pip install django
## 제일 밑에 warning 사인이 뜨는데,
pip install --upgrade pip # 를 이용해 pip을 업그레이드 해주면 해결할 수 있다!
# 사용할 수 있는 django 명령어 조회
django-admin
# 현재 위치에 pragmatic_drf 라는 django 프로젝트 생성 (얘가 메인 장고 프로젝트)
django-admin startproject pragmatic_drf .
# manage.py 를 실행시켜 서버 가동 (127.0.0.1:8000 접속)
python manage.py runserver
pip install djangorestframework
pip install markdown # browser를 통해 api를 테스트 하기 위함
# settings 파일 설정
# pragmatic_drf/settings.py
INSTALLED_APPS = [
...
'rest_framework',
]
# url 파일 설정
# pragmatic_drf/url.py
path('api-auth/', include('rest_framework.urls'))
urlpatterns = [
...
# browser를 통해 테스트할 때 인증을 위한 과정이 필요해서
# 파일을 가져올 땐 include 쓴다
path('api-auth/', include('rest_framework.urls'))
]
: MVC (x) MVT (o) = Model-View-Template 패턴
import : Command + .
python manage.py startapp accountapp # 라우팅을 위해 accountapp 폴더 생성
# url.py
path('accounts/', include('accountapp.urls'))
# settings.py
INSTALLED_APPS = [
...
'accountapp',
]
from django.urls import path
from accountapp.views import hello_world, hello_world_drf
urlpatterns = [
path('hello/', hello_world),
path('hello_drf/', hello_world_drf),
]
from urllib import response
from django.http import HttpRequest, HttpResponse
from django.shortcuts import render
from rest_framework.decorators import api_view
from rest_framework.response import Response
# Create your views here.
# 기존 장고 방식
def hello_world(requset):
return HttpResponse('Hello World!')
# DRF 방식
@api_view()
def hello_world_drf(request):
return Response("Hello DRF")
기존 장고 방식 출력
DRF 방식 출력