Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 17ca2aa

Browse files
Instructions for environment variables
1 parent f74bf63 commit 17ca2aa

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

‎units/django/4/database-orm-and-models.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,27 @@ You can try manipulating the models using the shell and can refresh the browser
315315

316316
Awesome. Hope, this tutorial explained well what Models in Django are and how it abstracts away the complexities of the underlying database.
317317

318+
## ProTip
319+
In our above tutorial we've stored our database credentials directly in our `settings.py` which is not a good practice if we're going to check in that file in our git repository.
320+
321+
It's a recommended practice to load your credentials or deployment specific configurations using environment variables or some other gitignored file and just reference them in your `settings.py` file something like this:
322+
323+
```python
324+
DATABASES = {
325+
'default': {
326+
'ENGINE': 'django.db.backends.postgresql',
327+
'NAME': os.environ.get('DB_NAME'),
328+
'USER': os.environ.get('DB_USER'),
329+
'HOST': os.environ.get('DB_HOST'),
330+
'PORT': os.environ.get('DB_PORT'),
331+
'PASSWORD': os.environ.get('DB_PASSWORD'),
332+
}
333+
}
334+
```
335+
You can take this [sample](https://github.com/kabirbaidhya/django-todoapp/blob/step-16/todoapp/settings.py#L79-L88) as an example.
336+
Here I've made use of a python package `python-dotenv` to load environment variables from a `.env` file.
337+
338+
## Source Code
318339
Check the full source code [here](https://github.com/kabirbaidhya/django-todoapp/tree/step-16).
319340

320341
## Read More?

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /