第一种形式:
1、项目设置中配置(settings.py)
STATIC_ROOT = os.path.join(BASE_DIR, ‘static‘)
2、在全局url中配置(urls.py)
re_path(r‘^static/(?P<path>.*)$‘, static.serve, {‘document_root‘: settings.STATIC_ROOT})
3、执行资源搜集命令
python manage.py collectstatic
4、模板中使用
<link rel="stylesheet" type="text/css" href="{% static ‘mysite/style.css‘ %}" />
5、项目结构图
第二种形式:
1、在应用url中配置(urls.py)
re_path(r‘^static/(?P<path>.*)$‘, static.serve, {‘document_root‘: ‘./static/‘}, name=‘static‘)
2、模板中使用
<link rel="stylesheet" type="text/css" href="{% url ‘mysite:static‘ ‘mysite/style.css‘ %}" />
3、项目结构图
参考:https://docs.djangoproject.com/zh-hans/2.0/ref/contrib/staticfiles/
原文:http://blog.51cto.com/xuke1668/2147243