Django1.5中设置静态目录的简要说明
- 1. 打开项目的settings.py配置文件,作如下改动:
-
- import os # 增加引入
- CURRENT_DIR = os.path.dirname(__file__).replace('\\','/') #增加当前目录变量
- STATIC_ROOT = '' #暂不改动
- STATIC_URL = '/static/' # 或其他也可
- STATICFILES_DIRS = (
CURRENT_DIR +STATIC_URL,) #增加一项
- 2. 打开项目的urls.py问价,作如下改动:
-
- from django.contrib.staticfiles.urls import staticfiles_urlpatterns # 增加引入
- urlpatterns += staticfiles_urlpatterns() # 增加一行urlpatterns
- from django.contrib.staticfiles.urls import staticfiles_urlpatterns # 增加引入
- 3. 在templates里使用静态文件了:
<script type="javascript/text" src="/static/js/config.js"></script>
<link rel="stylesheet" type="text/css" href="/static/css/contents.css"/>
<img src="/static/images/logo.ipg" alt=""/>
使用的时候注意 路径的开头需要加上“/”
When you’re ready to move out of local development and deploy your project:
-
Set the STATIC_URL setting to the public URL for your static files (in most cases, the default value of/static/ is just fine).
-
Set the STATIC_ROOT setting to point to the filesystem path you’d like your static files collected to when you use the collectstatic management command. For example:
STATIC_ROOT = "/home/jacob/projects/mysite.com/sitestatic"
-
Run the collectstatic management command:
./manage.py collectstatic
This’ll churn through your static file storage and copy them into the directory given by STATIC_ROOT.
-
Deploy those files by configuring your webserver of choice to serve the files in STATIC_ROOT atSTATIC_URL.
Serving static files in production covers some common deployment strategies for static files.
Those are the basics. For more details on common configuration options, read on; for a detailed reference of the settings, commands, and other bits included with the framework see the staticfiles reference.