[django] cheat sheet

Yeseul Han·2024년 7월 16일
0

CORS

1. django-cors-headers 패키지 설치

2. settings.py 설정

# settings.py

INSTALLED_APPS = [
    ...
    'corsheaders',
    ...
]

MIDDLEWARE = [
    ...
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
]

# CORS 허용 설정
CORS_ORIGIN_ALLOW_ALL = False  # 모든 도메인을 허용하지 않음
CORS_ORIGIN_WHITELIST = [
    'http://127.0.0.1:5500',  # 허용하고자 하는 도메인 추가
    'http://localhost:5500',
]

# 추가 설정 (필요한 경우)
CORS_ALLOW_CREDENTIALS = True

오랜만에 프론트엔드 붙이니까 다음과 같이 에러가 남.

  1. The Cross-Origin-Opener-Policy header has been ignored, because the URL's origin was untrustworthy. It was defined either in the final response or a redirect. Please deliver the response using the HTTPS protocol. You can also use the 'localhost' origin instead.
    => 장고 내부에서 call하는거라서 localhost 로 fetch 해봤음.
profile
코딩 잘하고 싶다

0개의 댓글