Skip to content Skip to sidebar Skip to footer

Django Is Not Serving Static And Media Files In Development But It Is Serving In Production

I am using windows 10 as OS in development environment and Ubuntu 18.04 (AWS) in production. I deployed my application recently (15 days) but now when I see the django is no more s

Solution 1:

if you want to serve static files during development:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

this setting is the only thing you need in your setting file which i assume your STATIC_URL is defined as /static/ , you comment out those lines and it will work.

I took these lines from documentation. Hence you can use seperate settings files for production and development of django also. so one will have DEBUG=True while the other one is defined False, which I think that is the reason that your problem is occurs.

ps: according to your BASE_DIR setting. add two lines to your development settings in settings.py file

STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn')

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

and for the urls.py I use these lines

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
urlpatterns +=  static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Solution 2:

After much effort, I think I have found the answer. In the development server the following configurations works:

STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = ('static', )

While in production server with Nginx properly set up to serve the static files the following configuration is enough:

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

Post a Comment for "Django Is Not Serving Static And Media Files In Development But It Is Serving In Production"