django:访问本地静态文件的配置

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

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('\\', '/')),
)

二、在url.py中增长配置css

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns += staticfiles_urlpatterns()

三、在html中引入静态文件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>
相关文章
相关标签/搜索