django遇到css加载不成功的问题

一、问题

resource interpreted as stylesheet but transferred with mime type text/html


原因:css文件加载不成功


二、解决方法

将css的文件放在static目录下,static和templates在同一个目录。结构图如下所示:

iot_app
├── admin.py
├── admin.pyc
├── __init__.py
├── __init__.pyc
├── migrations
│   ├── 0001_initial.py
│   ├── 0001_initial.pyc
│   ├── 0002_auto_20150317_1623.py
│   ├── 0002_auto_20150317_1623.pyc
│   ├── __init__.py
│   └── __init__.pyc
├── models.py
├── models.pyc
├── static
│   ├── css
│   │   ├── bootstrap.css
│   │   ├── bootstrap.css.map
│   │   ├── bootstrap.min.css
│   │   ├── bootstrap-theme.css
│   │   ├── bootstrap-theme.css.map
│   │   └── bootstrap-theme.min.css
│   ├── fonts
│   │   ├── glyphicons-halflings-regular.eot
│   │   ├── glyphicons-halflings-regular.svg
│   │   ├── glyphicons-halflings-regular.ttf
│   │   ├── glyphicons-halflings-regular.woff
│   │   └── glyphicons-halflings-regular.woff2
│   ├── images
│   │   ├── chrome.png
│   │   ├── firefox.png
│   │   ├── ie.png
│   │   ├── opera.png
│   │   └── safari.png
│   └── js
│       ├── bootstrap.js
│       ├── bootstrap.min.js
│       ├── html5shiv.js
│       ├── html5shiv.min.js
│       ├── jquery-1.11.1.js
│       ├── jquery-1.11.1.min.js
│       ├── jquery-1.11.1.min.map
│       ├── jquery-1.11.2.min.js
│       ├── jquery-1.11.2.min.map
│       ├── npm.js
│       └── respond.min.js
├── templates
│   ├── base.html
│   ├── buttons.html
│   ├── contact.html
│   ├── form.html
│   ├── form_inline.html
│   ├── formset.html
│   ├── form_using_template.html
│   ├── index.html
│   ├── login.html
│   ├── pagination.html
│   └── tabs.html
├── tests.py
├── views.py
└── views.pyc
设置setting.py

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static/'),
)


在html文件中,加载css或者js的地方写成如下形式:

{% load staticfiles %}
<link rel="stylesheet" href="{% static 'assets/css/amazeui.min.css' %}">
<link rel="stylesheet" href="{% static 'assets/css/admin.css' %}">
<script src="{% static 'assets/js/jquery.min.js' %}"></script>
<script src="{% static 'assets/js/amazeui.min.js' %}"></script>
<script src="{% static 'assets/js/app.js' %}"></script>


记得在开头加载一下static文件:

{% load staticfiles %}


另外要注意的是:

(1)setting.py中的INSTALLED_APPS中包含django.contrib.staticfiles

(2)setting.py中DEBUG选项为True,否则无法映射到静态文件目录

  • 11
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值