Django는 파이썬기반 웹어플리케이션 프레임웤이다. 이 글은 설정 작업중 html파일, js파일, css파일등 "static resources"라고 하는 것들을 URL에 직접 맵핑하는 방법을 설명한다.
일반적으로 'settings.py'파일에 'STATIC_URL', 'STATICFILE_DIR'등을 설정하는데, URL이 'STATIC_URL'에 정의된 경로로 제한된다. 만약 메인페이지 "/index.html"을 static 파일을 사용하려면, 약간 까다롭게 된다.
좀더 유연하고 유용한 방법으로, "urls.py"파일에 정의하는 urlpatterns 배열에 'path','url'등을 설정할 때 static resource를 설정하는 방법을 설명한다.
urls.py
from django.conf.urls import url
from django.urls import path
from django.conf import settings
from django.views import static
urlpatterns = [
path('index.html', static.serve, {'document_root':settings.UIUX_DIR, 'path':settings.INDEX_HTML_PATH}),
url(r'^biz/(?P<path>.*)$', static.serve, {'document_root':settings.BIZ_ROOT}),
url(r'^mybiz/', static.serve, {'document_root': settings.BIZ_ROOT}),
]
'urlpatterns' 배열에 3개의 설정이 있다. 하나는 'path', 다른 두개는 'url'을 이용한 것이다. 그리고 마지막 'url'설정은 오류가 발생한다. "settings.UIUX_DIR"등의 값은 'settings.py'파일에 설정된 값으로 파일 경로등을 담고 있다.
INDEX_HTML_PATH = 'index.html'
BIZ_ROOT = os.path.join(UIUX_DIR, 'biz')
템플릿으로 "static.serve"로 설정하고, 매개변수로 'document_root'등의 값을 넘겨주면 된다.
error
'url'을 사용할 때, 정규식에서 path값이 설정되도록 해야한다. 위의 'urlpatterns'설정중 마지막 것은 브라우저에서 호출시 다음과 같은 오류가 발생한다.
serve() missing 1 required positional argument: 'path'
오류 메시지를 통해서도 알 수 있듯이, "static.serve" 템플릿은 'document_root'와 'path' 두개의 변수를 필요로 한다.
'Trouble Shooting' 카테고리의 다른 글
Angular, error TS2339: Property 'map' does not exist on type 'Observable<Object>' (0) | 2018.07.30 |
---|---|
안드로이드 새모듈 생성, 빌드시 오류 (0) | 2018.05.08 |
nashorn, 외부 JSON객체 파싱 및 할당하기 (0) | 2018.04.12 |
Android Studio 3.1, 데이터바인딩 오류 (0) | 2018.04.02 |
자바스크립트, 싱글톤(Singleton) 객체 만들기 (0) | 2018.03.24 |