Sammu Sundar
Sammu Sundar

Reputation: 606

Static css and images not loading in django

I am new to Django. I ran the project in Debug = True at that time all CSS and images are loading properly. I try to change Debug = False. Now I face problems. Sometimes CSS is not loading . some times images are not loading. I have two images in the folder. One is loading and the other is not loading. In Develop mode Debug = True at that time is working properly.

I tried restarting python3 manage.py runserver, changing STATIC_URL = "../../static/" with no luck. Some images are loaded, some are not.

settings.py

STATIC_URL = "/static/"

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

mysite/static/images/icon/1.png //come
mysite/static/images/icon/2.png //not come

Both 1.png and 2.png are in folder. Sometimes CSS is loading, sometimes not.

I am using python3 manage.py runserver 0.0.0.0:5000 to start the project

Upvotes: 2

Views: 619

Answers (1)

Enderson Menezes
Enderson Menezes

Reputation: 479

See the link that the friend posted, see the django documentation.

I use Django with NGINX with the following configuration:

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

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

Django will fetch the static files in URL /static/ and the MEDIA files in URL /media/, so your NGINX has to be configured to search for those files:

location /static/ {
        alias   /your-project-folder/static/;
}

location /media/ {
        alias   /your-project-folder/media/;
}

Upvotes: 2

Related Questions