This repository contains a Python implementation of an environment variable validator inspired by the Create T3 App documentation for Next.js applications. The Python version is with example Django like env variables. Adjust it to suite your needs.
The environment variable validator uses the Pydantic library to validate Django-specific environment variables at runtime. This ensures that your Django application only runs if the required environment variables are properly set.
The example validation includes the following Django environment variables:
- TIME_ZONE
- DATABASE_URL
- DEBUG
- Define the Django environment variables schema in the
Envclass with appropriate validators. - Read environment variables from the
.envfile using theoslibrary. - Validate the environment variables using the
Envschema. - If the environment variables are valid, store them in the
envobject.
You can then import the env object in your Django application and use the validated environment variables.
from env_validator import env
# Access TIME_ZONE environment variable
time_zone = env.TIME_ZONE
# Access DATABASE_URL environment variable
database_url = env.DATABASE_URL
# Access DEBUG environment variable
debug = env.DEBUG