May 6, 2024, 6:41 a.m. | Agar Joshua

DEV Community dev.to

Many resources can be unclear when explaining how to set up environment variables in Django with '.env' files. Here's a straightforward guide:



  1. Download the Python Dotenv library:



pip install python-dotenv



  1. Add or edit your '.env' file:



ENV_KEY = "your-key-goes-here"



  1. Import it in your specific file(The file you want to include your '.env' file values):



import os
from django.core.exceptions import ImproperConfigError
from django.views import View

#Import load_dotenv module
from dotenv import load_dotenv

class KeyValueView(View):
def get(self, request):
"""
Gets a key …

backend django download edit env environment file files guide import install key library pip python resources set variables webdev

Software Engineer for AI Training Data (School Specific)

@ G2i Inc | Remote

Software Engineer for AI Training Data (Python)

@ G2i Inc | Remote

Software Engineer for AI Training Data (Tier 2)

@ G2i Inc | Remote

Data Engineer

@ Lemon.io | Remote: Europe, LATAM, Canada, UK, Asia, Oceania

Artificial Intelligence – Bioinformatic Expert

@ University of Texas Medical Branch | Galveston, TX

Lead Developer (AI)

@ Cere Network | San Francisco, US