简体   繁体   中英

How does a .env file relate to Python / Django?

I'm pretty new to Django / Python, and I'm trying to figure out how a .env file relates to a Django project.

Example .env :

DATABASE_URL=postgres://postgres_user@db:xxxx/postgres_db
DJANGO_SETTINGS_MODULE=spare.settings.dev
SECRET_KEY=example

I did manage to find this Stack Overflow post , which gives some information, but was hoping for a bit more.

  • Do all Django projects have a .env file?
  • Do non-Django python projects have a .env file, or is it generally a Django-related thing?
  • Where is the .env file typically being called from? In other words, how does the rest of the project know that the .env file exists?

We can only guess because we don't have access to your actual environment.

The .env file may be a container manager thing or something from libraries like python-decouple - for practical effects the .env will be used to populate the environment variables when the container "boots" or will be used to fill instance settings.

There is a common pattern made popular by the Twelve-Factor app : the item III is "Store config in the environment". Then in the settings.py file you use the KEY = os.environ.get('KEY', 'defaul_value') . The idea is to separate instance settings from project settings from code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM