Serve static files with Django from both CDN and local directories
Is it possible to serve static files from both a CDN and a local directory? Currently STATIC_URL points to CDN. I also configured STATICFILES_DIR to work with files from local directories. Also django.contrib.staticfiles is in INSTALLED_APPS. However, if I say:
"{% static "img/some_image.png" %}"
in my template, Django tries to download the file from the CDN and never tries to locally search for it. Is there a way to serve in both places?
source to share
No, he can not. It will always build a URL from the value STATIC_URL
.
What you can do is put a flag in settings.py
, so if you are running on localhost, change the variable STATIC_URL
:
if DEBUG:
STATIC_URL = '/static/'
Similarly, in 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)
Additional tips are available in the tutorial , including similar code for serving media files (user-uploaded files) during development.
source to share