[Answered ]-Can't load image in my django template

2πŸ‘

βœ…

For the given directory structure, You can put your static files on same level as of the directory with file, mr_doorbeen/settings.py

mrdoorbeen
manage.py
    mr_doorbeen
      setting.py
    mrdoorbeen
      migrations
      templates
         index.html
         profile
             profile.html
    static/

You can set the static files to the said location as follows.

# Static files configurations.
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

0πŸ‘

The problem is with your settings file where you need to define STATIC_URL. Just follow the description in Django’s documentation.

πŸ‘€hybor

0πŸ‘

I had this same problem and solved it by adding to my settings file the import of os from pathlib and the STATICFILES_DIRS line provided in one of the other responses:

from pathlib import Path, os

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
    )

STATIC_URL was already properly specified to β€˜/static/’. I couldnt solve the problem with just the STATICFILES line until I realized os is a function in pathlib that needed to be imported first.

πŸ‘€tfruda

0πŸ‘

Also, in addition to importing os and adding the line just mentioned, copying the image files under static directly from within a folder that was under static (named after the app) was key to making it work. I kept the files in the folder under static too so there was redundancy.

After I did that, it worked, and then I could then delete the image files under static directly (not touching those within the folder under static) and it still worked. Somehow, copying them directly under static "taught" Django to access the files within the folder under static, which I dont understand, but it worked.

πŸ‘€tfruda

Leave a comment