Programming/장고(django) - API
장고(django)/ 기본설정
esoog Polaris
2023. 6. 2. 00:14
# 장고 프로젝트폴더 내 settings.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
# 시스템 경로값 사용을 위해서 | |
from pathlib import Path | |
BASE_DIR = Path(__file__).resolve().parent.parent | |
# Path(__file__) : 현재 실행 파일의 경로 | |
# .resolve() : 절대경로 반환 | |
# .parent.parent : 부모의 부모폴더. 그러니까 장고 전체 디렉토리를 베이스 디렉토리로 | |
DEBUG = True | |
# 실제 서버 오픈 시에는 디버그 모드를 False로 | |
ALLOWED_HOSTS = ['*'] | |
# host 접속이 가능한 값. '*' 모두 | |
# Application definition | |
INSTALLED_APPS = [ | |
'django.contrib.admin', | |
'django.contrib.auth', | |
'django.contrib.contenttypes', | |
'django.contrib.sessions', | |
'django.contrib.messages', | |
'django.contrib.staticfiles', | |
# 앱 추가시 여기에 사용 앱 이름 입력 | |
] | |
TEMPLATES = [ | |
{ | |
'BACKEND': 'django.template.backends.django.DjangoTemplates', | |
'DIRS': [os.path.join(BASE_DIR, 'templates')], | |
# 템플릿 파일 경로 기본 설정. | |
# os.path.join( ) 값을 합쳐서 경로로 반환하는 함수 | |
# os.path.basename( ) 경로에서 파일 이름만 | |
'APP_DIRS': True, | |
'OPTIONS': { | |
'context_processors': [ | |
'django.template.context_processors.debug', | |
'django.template.context_processors.request', | |
'django.contrib.auth.context_processors.auth', | |
'django.contrib.messages.context_processors.messages', | |
], | |
}, | |
}, | |
] | |
DATABASES = { | |
'default': { | |
'ENGINE': 'django.db.backends.sqlite3', | |
'NAME': BASE_DIR / 'db.sqlite3', | |
} | |
} | |
# 디폴트 데이터베이스 설정. 여러 개의 데이터베이스 파일 생성 가능 | |
# 단, 설정은 따로 참고 필요. | |
LANGUAGE_CODE = 'ko' | |
TIME_ZONE = 'Asia/Seoul' | |
USE_I18N = True | |
USE_TZ = True | |
# 정적파일 경로 | |
STATIC_ROOT = os.path.join(BASE_DIR, 'static') | |
# 저장 디렉토리 | |
STATIC_URL = '/static/' | |
# 접근 url | |
# 동적파일 | |
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') | |
MEDIA_URL = '/media/' | |
# DRF 사용 설정(페이지네이션) | |
REST_FRAMEWORK = { | |
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', | |
# 페이 번호로 페이지 네이션 클래스 | |
'PAGE_SIZE': 10 | |
# 페이지당 10개 목록으로 | |
} |
# 장고 프로젝트폴더 내 urls.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib import admin | |
# 관리자 모드 사용 | |
from django.urls import path | |
# urls 패턴에서 경로 라우팅 하는 함수 | |
# 웹url과 연결고리를 만들어준다. | |
# 앱별로 분할 필요 | |
urlpatterns = [ | |
path('admin/', admin.site.urls), | |
# 원하는 'url패턴 경로값'을 적고, | |
path('', main.views.start_view), | |
# 입력->url패턴, 출력->실현 함수(앱.views.함수) | |
# 함수는 이름만 입력 | |
path('main/', main.views.Main.as_view()), | |
# 클래스 범위에서 사용하려면 () 객체생성 필요 | |
path('images/<str:image_name>/',contents.views.get_image) | |
# 여기서 'images/' 경로 다음의 <str 문자열은 : image_name 이라는 변수로 사용>/ 하는 url 맵이다. | |
# 실행 뷰에서 위 변수를 받아서 사용할 수 있다. | |
] |
반응형