Paul R
Paul R

Reputation: 2797

Make two directories static in django

I have two directories. static contains javascript and css files. In settings I wrote STATIC_URL = '/static/' so they are included correctly.

But also I have folder images which has pretty many images. My question how can I make folder images also static, because I can't copy into into static.

Thank you.

Upvotes: 11

Views: 17463

Answers (3)

Roddy P. Carbonell
Roddy P. Carbonell

Reputation: 865

I wanted to add a static directory called uploads. I tried settings.py first, but it didn't work. However, I managed to turn uploads into a secondary static URL by adding this line to the file urls.py:

url(r'^uploads/(?P<path>.*)$', static.serve, {'document_root': settings.BASE_DIR + "/uploads"}),

If you get into trouble importing static.serve, this is the line you need to import it:

from django.views import static

Upvotes: 10

Luke Dupin
Luke Dupin

Reputation: 2305

If you want two different urls to be static, in my case the development and output directory for my angular2 app, this was my solution, inside your url.py:

from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = [
  # Examples:
  url(r'^', include('website.urls')),

  # Uncomment the next line to enable the admin:
  url(r'^admin/', include(admin.site.urls)),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

if settings.DEBUG:
  urlpatterns += static(settings.STATIC_DEBUG_URL, document_root=settings.STATIC_DEBUG_ROOT)

I have several settings.py files, then I symlink to the one I'm using based on the deployment. Inside my code.settings.py which links to settings.py for writing code, I added:

STATIC_DEBUG_URL = '/app/'
STATIC_DEBUG_ROOT = 'xxx/website/app/'

Upvotes: 2

dm03514
dm03514

Reputation: 55972

it looks like STATICFILES_DIRS can take multiple paths:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
    '/var/www/static/',
)

Upvotes: 19

Related Questions