From 46119c051dfc4dc08b88ae7998f87220ba4cfe90 Mon Sep 17 00:00:00 2001 From: Dishan Sachin <134765302+dishansa@users.noreply.github.com> Date: Tue, 14 Nov 2023 06:05:36 +0530 Subject: [PATCH 01/10] create .env.example file --- .env.example | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 00000000..c69baae7 --- /dev/null +++ b/.env.example @@ -0,0 +1,9 @@ +SECRET_KEY=my_secret_key +DEBUG=True + +DB_ENGINE=mysql +DB_HOST=127.0.0.1 +DB_PORT=3306 +DB_NAME=django +DB_USER=root +DB_PASSWORD= From 57ff9d24e82f9ef903d7194c4ea15a12d3b71f1f Mon Sep 17 00:00:00 2001 From: Dishan Sachin <134765302+dishansa@users.noreply.github.com> Date: Tue, 14 Nov 2023 06:14:35 +0530 Subject: [PATCH 02/10] Use environment variables for configuration Updated configuration settings, such as `SECRET_KEY`, `DEBUG`, and `ALLOWED_HOSTS`, by replacing hardcoded values with environment variables. --- hello_world/settings.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hello_world/settings.py b/hello_world/settings.py index ae8fa6ae..b76a1117 100644 --- a/hello_world/settings.py +++ b/hello_world/settings.py @@ -20,12 +20,12 @@ # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = "django-insecure-rc@04_mry_3-$@2sq$b9%-9jp6q2eyxf4bsw9&&esj++aw&r)p" +SECRET_KEY = os.getenv("SECRET_KEY") # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = os.getenv("DEBUG") -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS', '').split(',') if 'CODESPACE_NAME' in os.environ: codespace_name = os.getenv("CODESPACE_NAME") From 6c489283e761b91e4ef3a18029ebe1597a095b7c Mon Sep 17 00:00:00 2001 From: Dishan Sachin <134765302+dishansa@users.noreply.github.com> Date: Tue, 14 Nov 2023 06:21:32 +0530 Subject: [PATCH 03/10] add the ALLOWED_HOSTS variable --- .env.example | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.env.example b/.env.example index c69baae7..d36e9b05 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,9 @@ SECRET_KEY=my_secret_key DEBUG=True +# ALLOWED_HOSTS=yourdomain.com,anotherdomain.com (Each host is separated by a comma) +ALLOWED_HOSTS=* + DB_ENGINE=mysql DB_HOST=127.0.0.1 DB_PORT=3306 From 4f935b3937780a586c3dd9d7fc77f5eb23af6b47 Mon Sep 17 00:00:00 2001 From: Dishan Sachin <134765302+dishansa@users.noreply.github.com> Date: Tue, 14 Nov 2023 06:41:42 +0530 Subject: [PATCH 04/10] update the environment variables --- .env.example | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index d36e9b05..a316a018 100644 --- a/.env.example +++ b/.env.example @@ -4,9 +4,8 @@ DEBUG=True # ALLOWED_HOSTS=yourdomain.com,anotherdomain.com (Each host is separated by a comma) ALLOWED_HOSTS=* -DB_ENGINE=mysql DB_HOST=127.0.0.1 DB_PORT=3306 -DB_NAME=django -DB_USER=root -DB_PASSWORD= +DB_DATABASE="" +DB_USERNAME="" +DB_PASSWORD="" From 3ed5d0b144b028cebe16d29cde9205104ba9005a Mon Sep 17 00:00:00 2001 From: Dishan Sachin <134765302+dishansa@users.noreply.github.com> Date: Tue, 14 Nov 2023 07:11:08 +0530 Subject: [PATCH 05/10] add python-dotenv to requirements --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 6ec86a87..884ee6a9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ Django==4.1.13 django-browser-reload==1.6.0 +python-dotenv==1.0.0 From d351270aa2111efe39b72e9d31c74a56cd9a8f91 Mon Sep 17 00:00:00 2001 From: Dishan Sachin <134765302+dishansa@users.noreply.github.com> Date: Tue, 14 Nov 2023 07:15:35 +0530 Subject: [PATCH 06/10] use python-dotenv to access environment variables --- hello_world/settings.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hello_world/settings.py b/hello_world/settings.py index b76a1117..4b37bf14 100644 --- a/hello_world/settings.py +++ b/hello_world/settings.py @@ -11,6 +11,9 @@ """ import os from pathlib import Path +from dotenv import load_dotenv + +load_dotenv() # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent From 426896a4fd359ba43e4eeaa56cf731a8de6d905c Mon Sep 17 00:00:00 2001 From: Dishan Sachin <134765302+dishansa@users.noreply.github.com> Date: Tue, 14 Nov 2023 07:44:44 +0530 Subject: [PATCH 07/10] environment variables usage --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 72568262..90e4ac65 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ To collect static files: ```python python manage.py collectstatic ``` +Create a `.env` file: +Create a new file named `.env` at the root of your project. Copy the contents from `.env.example` and adjust them as needed. To run this application: From 77a3450d8b65190f2658bcc83af2c17447425821 Mon Sep 17 00:00:00 2001 From: Dishan Sachin <134765302+dishansa@users.noreply.github.com> Date: Wed, 15 Nov 2023 20:24:12 +0530 Subject: [PATCH 08/10] rename the .env.example to .env in post create --- .devcontainer/devcontainer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 0f0ac41b..ff75c7a9 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -5,7 +5,7 @@ }, "waitFor": "onCreateCommand", "updateContentCommand": "pip install -r requirements.txt && python manage.py migrate", - "postCreateCommand": "", + "postCreateCommand": "cp .env.example .env", "postAttachCommand": { "server": "python manage.py runserver" }, From 783a0fc60114ec2a783c951fb828c539bb5a9e7d Mon Sep 17 00:00:00 2001 From: Dishan Sachin <134765302+dishansa@users.noreply.github.com> Date: Wed, 15 Nov 2023 21:11:40 +0530 Subject: [PATCH 09/10] rename the .env/ (directory) to .env (file) --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6ddaca13..71ea6a0c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ db.sqlite3 __pycache__/ staticfiles/ -.env/ +.env venv/ From 908f97d21e2c02fe050d5fcc9a19e2dcd8b43d88 Mon Sep 17 00:00:00 2001 From: Dishan Sachin <134765302+dishansa@users.noreply.github.com> Date: Wed, 15 Nov 2023 21:37:30 +0530 Subject: [PATCH 10/10] remove the instructions for setup the .env file --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 90e4ac65..2d3d97ee 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,6 @@ To collect static files: ```python python manage.py collectstatic ``` -Create a `.env` file: -Create a new file named `.env` at the root of your project. Copy the contents from `.env.example` and adjust them as needed. - To run this application: ```python