在django中将两个目录设为静态

时间:2013-08-26 14:48:16

标签: python django

我有两个目录。 static包含javascript和css文件。在设置中,我写了STATIC_URL = '/static/',因此它们被正确包含在内。

但我也有文件夹图像,它有很多图像。 我的问题如何使文件夹图像也是静态的,因为我无法复制到静态文件夹中。

谢谢。

3 个答案:

答案 0 :(得分:15)

看起来STATICFILES_DIRS可以采用多条路径:

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

答案 1 :(得分:2)

我想添加一个名为uploads的静态目录。我首先尝试了settings.py,但它没有用。但是,我设法通过将此行添加到文件urls.py:

,将上传内容转换为辅助静态URL
url(r'^uploads/(?P<path>.*)$', static.serve, {'document_root': settings.BASE_DIR + "/uploads"}),

如果您在导入static.serve时遇到问题,这就是导入它所需的行:

from django.views import static

答案 2 :(得分:1)

如果你想要两个不同的网址是静态的,在我的情况下我的angular2应用程序的开发和输出目录,这是我的解决方案,在你的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)

我有几个settings.py文件,然后我根据部署将符号链接到我正在使用的文件。在我的code.settings.py里面,它链接到settings.py来编写代码,我补充道:

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