码迷,mamicode.com
首页 > 其他好文 > 详细

django静态文件配置

时间:2015-05-22 11:48:59      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

最近研究django,发现如果使用cdn加速的css和js样式时候,网页打开速度有时会非常慢,因此我决定配置django静态文,以达到使用本地css和js样式目的,达到加速访问网页的效果。其他我就不多说了,直接上过程,我的django是1.7.7版本(亲测可行):

  • 配置settings.py文件,确保有如下内容:

STATIC_URL = ‘/static/‘
SITE_ROOT=os.path.join(os.path.abspath(os.path.dirname(__file__)),‘..‘)
STATIC_ROOT = os.path.join(SITE_ROOT,‘static‘)
STATICFILES_DIRS = (
    ("css", os.path.join(STATIC_ROOT,‘css‘)),
    ("js", os.path.join(STATIC_ROOT,‘js‘)),
    ("images", os.path.join(STATIC_ROOT,‘images‘)),
)
  • 在django项目中,创建static文件夹,并从bootstrap官网下载用户生产环境的css和js等文件放入该文件夹中。

  我的django项目文件夹结构目录如下:

# tree
.
├── blog
│   ├── admin.py
│   ├── admin.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── templates
│   │   ├── index2.html
│   │   ├── index2.html.bak
│   │   ├── index.html
│   │   ├── login.html
│   │   ├── logout.html
│   │   └── regist.html
│   ├── tests.py
│   ├── views.py
│   └── views.pyc
├── db.sqlite3
├── manage.py
├── static
│   ├── css
│   │   ├── bootstrap.css
│   │   ├── bootstrap.css.map
│   │   ├── bootstrap.min.css
│   │   ├── bootstrap-theme.css
│   │   ├── bootstrap-theme.css.map
│   │   └── bootstrap-theme.min.css
│   ├── fonts
│   └── js
│       ├── bootstrap.js
│       ├── bootstrap.min.js
│       ├── html5shiv.js
│       ├── jquery-1.11.1.js
│       ├── jquery-1.11.1.min.js
│       ├── jquery-1.11.1.min.map
│       ├── npm.js
│       └── respond.min.js
└── test03
    ├── __init__.py
    ├── __init__.pyc
    ├── settings.py
    ├── settings.pyc
    ├── urls.py
    ├── urls.pyc
    ├── wsgi.py
    └── wsgi.pyc

修改模版文件中的一些网页文件,例如 index.html 等,修改类似如下内容:

...
<link href="/static/css/bootstrap.min.css" rel="stylesheet">
...
...
<script src="/static/js/jquery-1.11.1.min.js"></script>
<script src="/static/js/bootstrap.min.js"></script>
...

即可。



django静态文件配置

标签:

原文地址:http://my.oschina.net/coffeesa/blog/418043

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!