As a Python developer, I’ve worked on various Django projects where I came across managing environment variables. Environment variables hold sensitive data like API keys, database credentials, and debug settings. Printing these variables during development or debugging can save you a lot of headaches.
In this article, I’ll walk you through how to print Django environment variables easily and safely. Whether you’re debugging locally or checking configurations on a deployed app, these methods will help you access and display environment variables effectively.
Let’s get in!
Methods to Print Environment Variables in Django
Environment variables are the backbone of secure and flexible Django projects. They allow you to separate configuration from code, which is essential for deploying the same app across different environments (development, staging, production).
Sometimes, you need to verify if Django is reading these variables correctly. Printing them out is a quick way to confirm their values without exposing them in logs or error messages.
Read How to View Uploaded Files in Django
Method 1: Use Python’s os Module in Django Views
The simple way to print environment variables in Django is by using the built-in os module. You can access any environment variable using os.environ.get().
Here’s a simple example of printing environment variables within a Django view. Imagine you are working on an internal dashboard for a US-based company, and you want to confirm the AWS S3 bucket name your app is using.
# myapp/views.py
import os
from django.http import HttpResponse
def print_env_vars(request):
s3_bucket = os.environ.get('AWS_S3_BUCKET_NAME', 'Not Set')
secret_key = os.environ.get('DJANGO_SECRET_KEY', 'Not Set')
debug_mode = os.environ.get('DJANGO_DEBUG', 'Not Set')
response_text = (
f"AWS S3 Bucket: {s3_bucket}<br>"
f"Django Secret Key: {secret_key}<br>"
f"Debug Mode: {debug_mode}"
)
return HttpResponse(response_text)How to use:
- Add this view to your
urls.py - Visit the URL in your browser to see the environment variables printed
This method is quick and effective for development environments, but avoid printing sensitive information, such as secret keys, in production.
I executed the above example code and added the screenshot below.

Check out Python Django: Get Admin Password
Method 2: Print Environment Variables in Django Shell
Sometimes you want to check variables without creating a view. The Django shell is perfect for this.
Run:
python manage.py shellThen, inside the shell:
import os
print("AWS S3 Bucket:", os.environ.get('AWS_S3_BUCKET_NAME', 'Not Set'))
print("Django Secret Key:", os.environ.get('DJANGO_SECRET_KEY', 'Not Set'))
print("Debug Mode:", os.environ.get('DJANGO_DEBUG', 'Not Set'))This approach is useful for quick checks during development or troubleshooting.
I executed the above example code and added the screenshot below.

Read Upload Image File in Django
Method 3: Log Environment Variables
If you want to keep a record of environment variables without exposing them on the UI, logging is a safer option.
In your Django settings or any module, you can do:
import os
import logging
logger = logging.getLogger(__name__)
def log_env_vars():
logger.info(f"AWS S3 Bucket: {os.environ.get('AWS_S3_BUCKET_NAME', 'Not Set')}")
logger.info(f"Debug Mode: {os.environ.get('DJANGO_DEBUG', 'Not Set')}")Call log_env_vars() during startup or at a specific point in your app. Make sure your logging configuration writes info-level logs to a file or console where you can review them.
Check out Django vs ReactJS
Method 4: Use the django-environ Package
For more robust environment management, I recommend the django-environ package. It helps you read environment variables and cast them to the right types.
First, install it:
pip install django-environThen, in your settings.py:
import environ
env = environ.Env()
environ.Env.read_env() # reads the .env file
AWS_S3_BUCKET_NAME = env('AWS_S3_BUCKET_NAME', default='Not Set')
DJANGO_DEBUG = env.bool('DJANGO_DEBUG', default=False)
print(f"AWS S3 Bucket: {AWS_S3_BUCKET_NAME}")
print(f"Debug Mode: {DJANGO_DEBUG}")This method is great for projects where you want to centralize environment variables in .env files and access them cleanly.
Read Change Django Version in Python
Best Practices When Printing Environment Variables
- Never print sensitive data in production. Always sanitize or avoid printing secrets like passwords or API keys.
- Use logging with restricted access rather than HTTP responses for production-level debugging.
- Prefer environment variable management tools like
django-environto keep your code clean and secure. - Use environment variables to configure your app for different US regions or AWS accounts by changing
.envfiles or server configs.
Printing environment variables is a simple yet powerful way to debug and validate your Django configuration. Whether you choose to print them in views, the shell, or log files, always be mindful of security.
I hope this guide helps you confidently manage and inspect environment variables in your Django projects.
You may read:

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.