jm_p_op

aws Django 서버 배포하기 -4 admin csrf 토큰 넣기 본문

서버/aws

aws Django 서버 배포하기 -4 admin csrf 토큰 넣기

jm_p_op 2023. 6. 2. 02:00

참고자료 https://pypi.org/project/django-cors-headers/

python버전, django 버전에 따라 설정방식이 다르다.


1. venv 실행시킨후

pip install django-cors-headers

2. 중복시 조심

settings.py

INSTALLED_APPS = [
    ...,
    "corsheaders",
    ...,
]

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

3. 조건  3개중 하나 추가하기

  • CORS_ALLOWED_ORIGINS (주소)
  • CORS_ALLOWED_ORIGIN_REGEXES (정규표현)
  • CORS_ALLOW_ALL_ORIGINS (전)
CORS_ALLOWED_ORIGINS = [
    "https://example.com",]
    
CORS_ALLOWED_ORIGIN_REGEXES = [
    r"^https://\w+\.example\.com$",
]

CORS_ALLOW_ALL_ORIGINS=TRUE

 


CSRF 설정

CORS_ALLOWED_ORIGINS = [
    "https://read-only.example.com",
    "https://read-and-write.example.com (도메인)",
]

CSRF_TRUSTED_ORIGINS = [
    "https://read-and-write.example.com (도메인)",
]