[Django]-Django local_settings import error

4πŸ‘

βœ…

It doesn’t work because your forget the . in front of local_settings.py (relative import). Try from .local_settings.py import *, also check that there is an __init__.py in the current folder.

Concerning settings, I recommend you to store your settings in a settings folder. And create a setting file for each environment. Create a base.py file, which contains base settings and then import it in all other settings files.

Here is the structure :

β”œβ”€β”€ project_name
β”‚   β”œβ”€β”€ project_name
β”‚   β”‚   β”œβ”€β”€ settings
β”‚   β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”‚   β”œβ”€β”€ base.py
β”‚   β”‚   β”‚   β”œβ”€β”€ dev.py
β”‚   β”‚   β”‚   β”œβ”€β”€ staging.py
β”‚   β”‚   β”‚   └── prod.py
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ urls.py
β”‚   β”‚   └── wsgi.py
...

And a settings file (E.g :dev.py or local.py) :

try:
    from .base import *
except ImportError:
    print('Unable to import base settings file:')

# ...

Use always relative imports when it’s possible.

Concerning databases, you shouldn’t configure them in base.py. Declare them in each specific settings files.

2πŸ‘

from rororo.settings import inject_settings

in the end of your’s settings.py

inject_settings('settings_local', locals(), True)

Leave a comment