jazzband / dj-database-url

Use Database URLs in your Django Application.
https://pypi.org/project/dj-database-url/
BSD 3-Clause "New" or "Revised" License
1.48k stars 205 forks source link

Clarify the usage example in the docs #129

Closed nbensa closed 2 years ago

nbensa commented 4 years ago
DATABASES['default'] = dj_database_url.config(conn_max_age=600)

Raises:

    DATABASES['default'] = dj_database_url.config(
NameError: name 'DATABASES' is not defined

Correct code should be:

DATABASES = {
    'default': dj_database_url.config(conn_max_age=600)
}
jacobian commented 4 years ago

Yeah, I can see how that would be confusing.

It is, however, correct Python (hence my change to the title of the issue). It just assumes that someone's already defined DATABASES -- which is not common, but not super uncommon either. For example, something like this is fairly typical:

DATABASES = {'default': {'engine': 'django.db.backends.sqlite3', 'name': 'something.db'}}

# ... later ...

if 'DATABASE_URL' in os.environ:
    DATABASES['default'] = dj_database_url.config()

Nevertheless, the example should probably be a bit more clear (or contain both versions). If you'd like to work up a PR I'd love to review it!

ParthS007 commented 4 years ago

@jacobian I can make a PR for that.

erictheise commented 4 years ago

For clarity I'd simply replace what's currently in the README with @nbensa's "Correct code". Someone who's already defined DATABASES should be able to follow along. I'm okay with @ParthS007's solution, too, either is an improvement.