settings配置
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, '/static/'),]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
收集静态文件
python manage.py collectstatic
urls配置
from django.conf.urls import url
from django.views import static
urlpatterns = [
url(r'^static/(?P<path>.*)$', static.serve,
{'document_root': settings.STATIC_ROOT}, name='static'),
]
管理静态文件官方文档
https://docs.djangoproject.com/en/2.2/howto/static-files/
真正部署的时候,静态文件一般放在web服务器(例:nginx),先在本地收集静态文件,然后推送到服务器
生产环境部署官方文档
https://docs.djangoproject.com/zh-hans/2.2/howto/static-files/deployment/