
# 계정 관리 기능을 담당하는 앱을 만들거라서 accountapp 명칭으로 앱을 생성
$ python manage.py startapp accountapp
프로젝트 내부에 {앱 이름}으로 된 패키지 폴더가 생성된다.
신규 앱을 만들면 이 앱을 사용할 수 있도록 하기 위해서, 메인 앱의 settings.py > INSTALLED_APPS에 아래와 같이 등록한다.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accountapp', ################## << 추가된 부분
]
간단히, 요청하면 'hello world'를 응답하는 함수를 만든다.
from django.http import HttpResponse
def hello_world(request):
return HttpResponse('hello world')
이렇게 만든 함수를 특정 라우터로 매핑하는 과정이 필요하다. 특정 엔드포인트로 호출을 했을시, 해당 함수가 실행될 수 있도록 하는 것이다.
먼저, 메인 앱의 urls.py에 작성된 부분이다.
path('account/', include('accountapp.urls')) : 'account/' 경로로 오는 모든 요청을 'accountapp.urls'로 전달한다는 의미
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('account/', include('accountapp.urls')) #### << 추가된 부분
]
신규 앱에는 urls.py가 따로 없으므로, 새로 추가해준다.
from django.urls import path
from accountapp.views import hello_world
app_name = "accountapp"
urlpatterns = [
path('hello_world/', hello_world, name='hello_world')
]
이렇게 View에 만든 함수를 라우팅해주게 되면, http://127.0.0.1:8000/account/hello_world/(=로컬에서 실행) 경로로 요청했을 때 hello_world 함수가 실행되며 그 응답값을 받게 된다.