DJANGO配置静态资源

时间:2023-01-28 20:43:27

回到Django,让静态资源起作用只需要简单的配置(下面的做法只适用于开发阶段):

修改settings.py的static files小节:

import os
... ...

# Additional locations of static files
HERE = os.path.dirname(__file__)
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
HERE+STATIC_URL,
)

然后在urls.py中增加static的url映射:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
... ...
# for development only
# This will only work if DEBUG is True.
urlpatterns += staticfiles_urlpatterns()