· 

Production: testing .env and settings.py

1.  Introduction

I created a .env file and modified the related lines in settings.py.  I think it will work well, but I am not sure it will.  How can I test the working of these files?

 

2. How about adding print() to settings.py?

In ~/django_business/proj01/proj01/settings.py, I wrote:

import os

import json                                     # for json.loads()

from dotenv import load_dotenv

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.

BASE_DIR = Path(__file__).resolve().parent.parent

load_dotenv(BASE_DIR / ".env")

# Quick-start development settings - unsuitable for production

# See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!

SECRET_KEY = os.getenv("SECRET_KEY")    #reading from .env

# SECURITY WARNING: don't run with debug turned on in production!

#DEBUG = True

#DEBUG = False

DEBUG = (os.getenv("DEBUG") == "True")   # reading from .env: this converts a string into a boolean

#ALLOWED_HOSTS = []

#ALLOWED_HOSTS = ['*']

ALLOWED_HOSTS = json.loads(os.getenv("ALLOWED_HOSTS"))   # reading from .env: this converts a string into a list

In ~/django_business/proj01/.env, I wrote:

# SECURITY WARNING: keep the secret key used in production secret!

SECRET_KEY = "my-secret-key-very-long"

 # SECURITY WARNING: don't run with debug turned on in production!

DEBUG = True

ALLOWED_HOSTS = []

Now I want to test if settings.py reads the data from .env correctly.  One way I think of is to add "print(SECRET_KEY, DEBUT, ALLOWED_HOSTS)" in settings.py, but where will the results be printed?  Or, are there better ways to check it?  Give me any suggestions.

 

You're on the right track, and your question is very practical for Django development.

✅ Where print() goes in settings.py

If you add this line in settings.py:

python
print(SECRET_KEY, DEBUG, ALLOWED_HOSTS)

It will be printed to the console when Django starts up, such as:

  • when you run python manage.py runserver

  • or when you run python manage.py check, migrate, etc.

So yes, using print() is a perfectly fine way to debug whether values from .env are loaded correctly.


✅ Alternative and better way: python manage.py shell

Run this command:

bash
python manage.py shell

Then enter:

python
from django.conf import settings print(settings.SECRET_KEY) print(settings.DEBUG) print(settings.ALLOWED_HOSTS)

This is a cleaner and safer way to check, and it avoids editing settings.py directly.


✅ Bonus: Your .env and code are good, but here's a tiny fix

In your .env file:

dotenv
DEBUG = True # <- this is a Python-style boolean, but .env files store strings ALLOWED_HOSTS = [] # <- this is treated as a string, not a list

To avoid confusion or parsing issues with json.loads(), change it to:

dotenv
DEBUG = "True" ALLOWED_HOSTS = '["127.0.0.1", "localhost"]' # or ["*"] for all hosts

And then keep this line in settings.py:

python
ALLOWED_HOSTS = json.loads(os.getenv("ALLOWED_HOSTS"))

✅ Summary

  • Yes, print() in settings.py works; output appears in the terminal when Django runs.

  • Better: use python manage.py shell and settings.<VAR> to check values.

  • Fix .env to use JSON-compatible formats when using json.loads().

 

Would you like help writing a test view that shows these values in the browser (only in debug mode)?

 

3.  I tried both methods

(1) In settings.py, I added:

print(SECRET_KEY, DEBUG, ALLOWED_HOSTS)     # run python manage.py check to test this

print(type(ALLOWED_HOSTS))                  # the same as above

Then, I ran:

python manage.py check

As a result, I got:

my-secret-key-very-long True ['myid.pythonanywhere.com', 'localhost']

<class 'list'>

 

(2) I used the shell, too.  I ran:

python manage.py shell

Then I ran some shell commands:

 

4. some changes in ALLOWED_HOSTS

I also made some changes in settings.py:

ALLOWED_HOSTS = os.getenv("DJANGO_ALLOWED_HOSTS").split(",")

 

In .env, I wrote:

DJANGO_ALLOWED_HOSTS=myid.pythonanywhere.com,localhost

 

It seems it worked.