Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,56 @@ mv <name-of-the-app> <django_project_root>/
Scattered throughout the Python and HTML of this project are places marked with "your stuff". This is where third-party libraries are to be integrated with your project.



---

### Running a Dockerized Django Project Locally Using Uvicorn

Follow these steps to run your Dockerized Django project locally using Uvicorn:

---

#### **Step 1: Update the Configuration to Use `.env` Files**
1. Open the `config/base.py` file in your project.
2. Locate the following line:
```python
READ_DOT_ENV_FILE = env.bool("READ_DOT_ENV_FILE", default=False)
```
3. Change the default value to `True`:
```python
READ_DOT_ENV_FILE = env.bool("READ_DOT_ENV_FILE", default=True)
```

This ensures the project reads environment variables from a single `.env` file.

---

#### **Step 2: Generate the `.env` File**
Use the provided utility scripts to merge existing environment files into a single `.env` file.

1. For **local development**, run:
```bash
python3 merge_local_dotenvs_in_dotenv.py
```

2. For **production**, create a `.env` file with production-specific values by running:
```bash
python3 merge_production_dotenvs_in_dotenv.py
```

This will generate a `.env` file in project root. Review the file and update database credentials or other values if necessary.

---

#### **Step 3: Run the Project with Uvicorn**
Start the project using Uvicorn:

```bash
uvicorn config.asgi:application --host 0.0.0.0 --reload --reload-include '*.html'
```

- **`--host 0.0.0.0`**: Makes the application accessible on all network interfaces.
- **`--reload`**: Enables auto-reloading for code changes.
- **`--reload-include '*.html'`**: Watches for changes in `.html` files for auto-reloading.

---
14 changes: 13 additions & 1 deletion {{dxh_py.project_slug}}/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,19 @@
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#databases
{% if dxh_py.use_docker == "y" -%}
DATABASES = {"default": env.db("DATABASE_URL")}
if READ_DOT_ENV_FILE:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": env("POSTGRES_DB"),
"USER": env("POSTGRES_USER"),
"PASSWORD": env("POSTGRES_PASSWORD"),
"HOST": env("POSTGRES_HOST"),
"PORT": env("POSTGRES_PORT"),
}
}
else:
DATABASES = {"default": env.db("DATABASE_URL")}
{%- else %}
{% if dxh_py.database_engine == 'postgresql' -%}
DATABASES = {
Expand Down
27 changes: 27 additions & 0 deletions {{dxh_py.project_slug}}/merge_local_dotenvs_in_dotenv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
from collections.abc import Sequence
from pathlib import Path

BASE_DIR = Path(__file__).parent.resolve()
PRODUCTION_DOTENVS_DIR = BASE_DIR / ".envs" / ".local"
PRODUCTION_DOTENV_FILES = [
PRODUCTION_DOTENVS_DIR / ".django",
PRODUCTION_DOTENVS_DIR / ".postgres",
]
DOTENV_FILE = BASE_DIR / ".env"


def merge(
output_file: Path,
files_to_merge: Sequence[Path],
) -> None:
merged_content = ""
for merge_file in files_to_merge:
merged_content += merge_file.read_text()
merged_content += os.linesep

output_file.write_text(merged_content)


if __name__ == "__main__":
merge(DOTENV_FILE, PRODUCTION_DOTENV_FILES)