Django1.5中设置静态目录的简要说明

网上查到不少关于设置静态目录的,版本有些差异,对新手而言,在Django1.5上总有这样那样的问题。例如一开始遇到的Settings不存在之类的(此时需要增加from django.conf import settings)。从相关文章中(具体链接见下)摘取整理相关内容,作为自己的新手笔记留存:
 
(一)开发阶段(DEBUG = True)
  • 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
  • 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=""/>

    使用的时候注意 路径的开头需要加上“/” 
(二)部署阶段(DEBUG = False)
 
在django1.5的doc里面有介绍如下(自己还没有试):
 

When you’re ready to move out of local development and deploy your project:

  1. Set the STATIC_URL setting to the public URL for your static files (in most cases, the default value of/static/ is just fine).

  2. 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"
  3. 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.

  4. 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.

 

转载于:https://www.cnblogs.com/hillfree/archive/2013/03/21/django15-static-dir-settings.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值