我将项目从Django 1.8升级到Django 1.10时遇到了麻烦:静态文件不再加载了。
我的模板如下所示:
STATIC_URL = os.path.join(BASE_DIR, 'static/')
STATIC_ROOT= os.path.join(BASE_DIR,'static/')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static/'), "./", ]
然后在我的文件settings.py中,我将'django.contrib.staticfiles'作为已安装的应用程序。 DEBUG设置为True,我有:
$location.hash
但是当生成html时,就像%static不再有效一样了。它被空字符串替换(同样适用于Django 1.8,其中%static被STATIC_URL的内容替换)。有谁知道如何解决这个问题?
答案 0 :(得分:1)
一旦你让chnages运行python manage.py collectstatic
命令,你能否添加更新urls.py(mainproject / urls.py)。
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
===================
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
def root(folder):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), '..',folder)
STATIC_ROOT = root('staticstorage')
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
root('static'),
)