Make two directories static in django

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

But I have folders that have quite a few images. My question is, how can I make folder images also static, because I cannot copy to static.

Thanks.

+4
source share
3 answers

it looks like STATICFILES_DIRS can take several paths:

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

If you want two different URLs to be static, in my case the development and output directory for my angular2 application, 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, and then I am symbolically attached to the one I am using, based on the deployment. Inside my code.settings.py, which references settings.py for writing code, I added:

 STATIC_DEBUG_URL = '/app/' STATIC_DEBUG_ROOT = 'xxx/website/app/' 
+1
source

I wanted to add a static directory called uploads. At first I tried settings.py, but this did not work. However, I managed to enable the download in the secondary static URL by adding this line to the urls.py file:

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

If you are unable to import static.serve, this is the line you need to import:

 from django.views import static 
0
source

Source: https://habr.com/ru/post/1498973/


All Articles