您的位置:首页 > 编程语言 > Go语言

django静态文件配置

2015-05-22 09:50 323 查看
最近研究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>
...

即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息