Manage Django config files with Python-decouple

Why you need it

Basically, many introductory articles hard-code various settings in the configuration file, but it is not good to think practically from the viewpoint of operation and security to hard-code so-called SECRET_KEY and environment variables directly. ..

So installing a module that can isolate the settings, managing it separately and loading from there is a way to avoid hard coding. In Python, there is a module called Python-decouple, so I'll use settings.py in Django as an example.

The project created this time can be viewed from here, so if you are interested, please.

Introduction

Install and

$ pip install python-decouple
$ django-admin startproject decoupleproject

Create a project.

Since you already have Python-decouple installed, create a .env file in the same directory as manage.py.

スクリーンショット 2020-06-13 14.53.09.png

And if you write the information you do not want to hard code in various configuration files in .env and import it in sttings.py, you can switch for each environment, which is very useful.

For example, this time

settings.py


from decouple import config


# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = config('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = conig('DEBUG' default=False,cost=bool)

ALLOWED_HOSTS = config('ALLOWED_HOSTS',cost=Csv())

.env


SECRET_KEY = '6@gw!zj8jjwjr%da0g=&1dzmhjbi3p%r@6157##n0oix#&ybv2'
DEBUG = True
ALLOWED_HOSTS = []

By writing in this way, you can separate information that you do not want to be known from the outside.

There are other modules that hide database connection information, so I'd like to introduce them in the near future. Thank you for your hard work.

Recommended Posts

Manage Django config files with Python-decouple
Upload files with Django
Handle csv files with Django (django-import-export)
Internationalization with django
CRUD with Django
Django: Record User Agent and manage with Admin
Configure a module with multiple files in Django
Authenticate Google with Django
Django 1.11 started with Python3.6
Development digest with Django
Output PDF with Django
Markdown output with Django
Use Gentelella with django
Getting Started with Django 1
Send email with Django
File upload with django
Use LESS with Django
Pooling mechanize with Django
Use MySQL with Django
Start today with Django
Getting Started with Django 2
[Django] Manage settings like writing in settings.py with a model
How to handle static files when deploying to production with Django
Do Django with CodeStar (Python3.6.8, Django2.2.9)
Manage cron jobs with python
Get started with Django! ~ Tutorial ⑤ ~
Minimal website environment with django
Create an API with Django
Manage python environment with virtualenv
Do Django with CodeStar (Python3.8, Django2.1.15)
Deploy Django serverless with Lambda
Python3 + Django ~ Mac ~ with Apache
Getting Started with Python Django (1)
Create a homepage with django
Sorting image files with Python (2)
Get started with Django! ~ Tutorial ④ ~
Sort huge files with python
Sorting image files with Python (3)
Getting Started with Python Django (4)
Web application creation with Django
Getting Started with Python Django (3)
Combine FastAPI with Django ORM
Get started with Django! ~ Tutorial ⑥ ~
Sorting image files with Python
Save tweet data with Django
Transfer files with teraterm [Note]
Integrate PDF files with Python
Reading .txt files with Python
Do AES encryption with DJango
Getting Started with Python Django (6)
Combine two images with Django
Getting Started with Django with PyCharm
About handling Django static files
Real-time web with Django Channels
Handle JSON files with Matlab
Double submit suppression with Django
Django REST framework with Vue.js
Use prefetch_related conveniently with Django
Getting Started with Python Django (5)
Login with django rest framework
Qiita API Oauth with Django