Search code examples
pythondjangodjango-settingsdjango-1.10django-4.1

How to tell Django it has to read a some_folder/local.py file as settings.py


I am building an app in Django 4.1.2, and I am following a tutorial of an app made with Django 1.10.3.

Right at the start, the teacher replaces settings.py with a directory settings containing the files.

  • local.py
  • production.py
  • base.py

each of these having as content the exact copy of settings.py, with the exception that production.py has DEBUG=False instead of True.

Then he changes, in each of these files, the BASE_DIR variable, from

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))

to

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

then, inside the new folder settings, he creates a __init__.py file, containing this:

from .base import *
from .production import *

try:
    from .local import *
except:
    pass

then he runs python manage.py runserver and the server runs correctly.

I did the same "file-to-folder" replacement, but I don't know how to update the content of BASE_DIR variable.

Actually, because of the superior version of Django in my project, BASE_DIR was built by Django as:

BASE_DIR = Path(__file__).resolve().parent.parent

How can I change it to have it correspond to the change made by the teacher?

I tried with:

BASE_DIR = Path(__file__).resolve().parent.parent.parent

then run python manage.py runserver, but it returns

Traceback (most recent call last):
  File "/home/tommaso/tommaso03/programmazione/corsi_udemy/my-twitterlike-app/src/my_twitterlike_app/manage.py", line 22, in <module>
    main()
  File "/home/tommaso/tommaso03/programmazione/corsi_udemy/my-twitterlike-app/src/my_twitterlike_app/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/tommaso/tommaso03/programmazione/corsi_udemy/my-twitterlike-app/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/home/tommaso/tommaso03/programmazione/corsi_udemy/my-twitterlike-app/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/tommaso/tommaso03/programmazione/corsi_udemy/my-twitterlike-app/venv/lib/python3.10/site-packages/django/core/management/base.py", line 415, in run_from_argv
    connections.close_all()
  File "/home/tommaso/tommaso03/programmazione/corsi_udemy/my-twitterlike-app/venv/lib/python3.10/site-packages/django/utils/connection.py", line 84, in close_all
    for conn in self.all(initialized_only=True):
  File "/home/tommaso/tommaso03/programmazione/corsi_udemy/my-twitterlike-app/venv/lib/python3.10/site-packages/django/utils/connection.py", line 76, in all
    return [
  File "/home/tommaso/tommaso03/programmazione/corsi_udemy/my-twitterlike-app/venv/lib/python3.10/site-packages/django/utils/connection.py", line 73, in __iter__
    return iter(self.settings)
  File "/home/tommaso/tommaso03/programmazione/corsi_udemy/my-twitterlike-app/venv/lib/python3.10/site-packages/django/utils/functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/tommaso/tommaso03/programmazione/corsi_udemy/my-twitterlike-app/venv/lib/python3.10/site-packages/django/utils/connection.py", line 45, in settings
    self._settings = self.configure_settings(self._settings)
  File "/home/tommaso/tommaso03/programmazione/corsi_udemy/my-twitterlike-app/venv/lib/python3.10/site-packages/django/db/utils.py", line 148, in configure_settings
    databases = super().configure_settings(databases)
  File "/home/tommaso/tommaso03/programmazione/corsi_udemy/my-twitterlike-app/venv/lib/python3.10/site-packages/django/utils/connection.py", line 50, in configure_settings
    settings = getattr(django_settings, self.settings_name)
  File "/home/tommaso/tommaso03/programmazione/corsi_udemy/my-twitterlike-app/venv/lib/python3.10/site-packages/django/conf/__init__.py", line 92, in __getattr__
    self._setup(name)
  File "/home/tommaso/tommaso03/programmazione/corsi_udemy/my-twitterlike-app/venv/lib/python3.10/site-packages/django/conf/__init__.py", line 79, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/tommaso/tommaso03/programmazione/corsi_udemy/my-twitterlike-app/venv/lib/python3.10/site-packages/django/conf/__init__.py", line 190, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'my_twitterlike_app.settings'

So it looks like it cannot find the settings.py file, but how can I tell Django that I have turned it into a folder containing three different files, or better, how can I tell Django it has to read settings/local.py instead of settings.py?


Solution

  • I had put by mistake the settings folder at path.

    my_twitterlike_app > settings

    instead of

    my_twitterlike_app > my_twitterlike_app > settings

    The server runs fine with:

    BASE_DIR = Path(__file__).resolve().parent.parent.parent
    

    in all the three settings file.