[Django]-Serve static files with Django both from CDN and local directories

0👍

No, it cannot. It will always build the url from the value of STATIC_URL.

What you can do is put a flag in settings.py, so that if you are running on your local host, to change the STATIC_URL variable:

if DEBUG:
   STATIC_URL = '/static/'

Similarly, in your urls.py, you can add this check as well:

from django.conf import settings  # add this to the top
from django.conf.urls.static import static 

urlpatterns = ('',
               url(r'home/$', ... ),
               # your other urls here
)
if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Further tips are available at the manual including similar code to serve media files (user uploaded files) during development.

Leave a comment