Home > front end >  Django doesn't show images after adding multiple environments
Django doesn't show images after adding multiple environments

Time:10-20

I'm trying to add local and production environments to my Django project. So instead of one settings.py file, I created a settings directory and in that directory, added 3 settings files: base.py, local.py, and pro.py

settings (directory)

  • base.py (the new name for settings.py)
  • local.py (inherits from base.py with some parameter overrides)
  • pro.py (inherits from base.py with some parameter overrides)

In the base.py file, I updated the BASE_DIR parameter to point to the parent folder of the current file.

previous BASE_DIR:

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

new BASE_DIR:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(
                            os.path.join(__file__, os.pardir))))

I didn't change static and media root parameters (in base.py):

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

However, after the change, my images don't show up anymore. What am I doing wrong?

CodePudding user response:

os.path.join(__file__, os.pardir) will result in something like /path/to/myproject/settings/base.py/...

If you've moved the base.py a level deeper (than the original settings.py), you can just add another os.path.dirname, E.g.

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