首页 > 技术文章 > django:访问本地静态文件的配置

jzss 2019-05-09 15:36 原文

1、在setting.py中新增如下配置,static为静态文件的目录,BASE_DIR为项目根目录

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
    ('images', os.path.join(STATIC_ROOT, 'images').replace('\\', '/')),
    ('js', os.path.join(STATIC_ROOT, 'js').replace('\\', '/')),
    ('css', os.path.join(STATIC_ROOT, 'css').replace('\\', '/')),
    ('fonts', os.path.join(STATIC_ROOT, 'fonts').replace('\\', '/')),
)

2、在url.py中增加配置

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns += staticfiles_urlpatterns()

3、在html中引入静态文件

{% load static %}
<script type="text/javascript" src="{%static 'js/jsencrypt.js'%}"></script>
<script type="text/javascript" src="{%static 'js/bootstrap.js'%}"></script>
<script type="text/javascript" src="{%static 'js/bootstrap.min.js'%}"></script>
<link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet" type="text/css">
<link href="{% static 'css/bootstrap.css' %}" rel="stylesheet" type="text/css">
<script type="image/gif" src="{%static 'images/loading.gif'%}"></script>

   

 

推荐阅读