728x90
반응형

 

각 잡고 공부한 건 아니고 프로젝트 코드 다시 보면서 복습 느낌으루다가…

pathlib 자주 사용하는 메소드 → from pathlib import Path


  • path.resolve() : 절대 경로 탐색
  • .parent : 상위 디렉토리
  • Path(’.’).resolve().parent : 현재 디렉토리의 바로 상위 디렉토리
  • Path 뒤에 ‘/’으로 경로를 직접 붙여서 입력할 수도 있다.
  • os.path.join() : 경로 조합
import os

dir_path = '/home/js/tests'
file_name = 'my_file.txt'

print(os.path.join(dir_path, file_name)) # /home/js/tests/my_file.txt

print(os.path.join(dir_path, "aaa/bbb", "ccc", file_name)) # /home/js/tests/aaa/bbb/ccc/my_file.txt

with ~ as문


  • f = open(’hello.txt’, ‘w’)와 f.close()를 동시에 하는 코드
  • write 안에는 string만 가능하기 때문에 str()을 활용해 type을 변경한 후 f.write 하기

swagger


  1. pip install -U drf-yasg : 가상환경 활성화 후 패키지 설치
  2. settings.py에 INSTALLED_APPS에 'django.contrib.staticfiles', 추가
  3. urls.py
...
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from django.urls import path, include, re_path

...

schema_view = get_schema_view(
   openapi.Info(
      title="Snippets API",
      default_version='v1',
      description="Test description",
      terms_of_service="<https://www.google.com/policies/terms/>",
      contact=openapi.Contact(email="contact@snippets.local"),
      license=openapi.License(name="BSD License"),
   ),
   public=True,
   permission_classes=[permissions.AllowAny],
)

urlpatterns = [
   re_path(r'^swagger(?P\\.json|\\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
   re_path(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
   re_path(r'^redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
   ...
]

path와 re_path


  • re_path() : django.conf.urls.url()과 동일
  • path() : Django가 기본적으로 제공하는 Path Converter를 통해 정규표현식 기입히 간소화 되었다.
반응형

+ Recent posts