1. 프로젝트 파일에 .env파일을 만들어준다
# .env
API_KEY ='내 키'
2. settings.py에서 추가해준다
# 여기에 내 PC 파일안에 저장된 API_KEY 변수를 가져옴
import environ
import os
env = environ.Env(
# set casting, default value
DEBUG=(bool, False)
)
environ.Env.read_env(os.path.join(BASE_DIR, '.env'))
DEBUG = env('DEBUG')
API_KEY = env('API_KEY')
참고 : https://django-environ.readthedocs.io/en/latest/quickstart.html
Quick Start - django-environ
Previous Installation
django-environ.readthedocs.io
3. views.py를 수정
from django.conf import settings
@api_view(['GET'])
def index(request):
api_key = settings.API_KEY
city = "Seoul,KR"
url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}'
response = requests.get(url).json()
return Response(response)'BackEnd > Django' 카테고리의 다른 글
| [Django] CORS Policy (0) | 2023.11.14 |
|---|---|
| [Django] API 문서화 (0) | 2023.10.19 |
| [Django] DRF(2) N:1 Relation (0) | 2023.10.19 |
| [Django] DRF(1) single model (0) | 2023.10.18 |
| [Django] DRF란? (0) | 2023.10.18 |