刚开始参考的是别的文章,后来参考文章《各种 django 静态文件的配置总结》才看到原来没有但是没有注意到版本,折腾了一晚上,浪费了很多很多时间.后来终于知道搜索django1.7访问静态文件.真是傻×.

环境:
python 2.7.3
django 1.7.5

django是不善于处理静态文件这种事情的.这样的工作要交给nginx或者apache这样的服务器.但是在调试时还是要配置一下的
django 1.7.5配置访问静态文件貌似比其他的版本都要简单一些.只需要如下步骤:

  1. 收集静态文件,然后放在app下的static中,目录如下:


    iot_app
    ├── admin.py
    ├── admin.pyc
    ├── forms.py
    ├── forms.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
  2. 设置setting.py
    STATIC_URL = '/static/'
    STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static/'),
    # '/var/www/static',
    )
  3. 在模版中使用
    {% load staticfiles %} < img src = "{% static "img/test.jpg" %}" alt = "test"/>
    或者
    {% load staticfiles %} < link rel="stylesheet" href="{% static 'css/header.css' %}" >

注意:

  • 确保setting.py中的INSTALL_APPS中包含django.contrib.staticfiles
  • 确保setting.py中的DEBUG选项为True,否则无法映射到静态文件目录

在生产模式中不可设置为True哦,所以生产模式下的配置是不一样的,下面使用nginx来配置静态资源.
补充:
说明一下,有关几个settings中设置的问题 static_root static_url staticfiles_dirs:

  1. settings.py中
    As for the static folder under myproject root, actually it doesn’t necessarily have to be there. As long as STATIC_ROOT (in settings.py) points to the same location. So what’s going on here is when you do:

    python manage.py collectstatic

    It will copy all the individual static folder to the STATIC_ROOT folder, that’s why in each of your app, you need to follow the naming convention like this:

    app_one/static/app_one/css

    You are not supposed to put stuff directly under the app’s static folder, which might cause name collisions when you collectstatic.
    也就是说.在执行collectstatic时,会自动的把每个app下的static文件拷贝到static_root指定的路径下.另外采用这样的目录格式app_one/static/app_one,这样在有多个app的情况下.可以就不会因为有文件名相同的文件被覆盖掉

  2. the STATIC_URL in the settings.py
    This url will appear in your browser:
    (BTW, make sure the URL ends with a slash/, without a slash it might work but very unstable, django will error out this in the log)

    # STATIC_URL = '/static/' # it doesn't have to this
    STATIC_URL = '/static/monkeyking/' # it can be anything

    You shouldn’t hardcode the img or css in your template something like:

    ... url = "/static/app_one/css/mycss.css" ...
    ... url = "../../static/app_one/img/logo.png" ...

    In stead, you should do this in your template:

    {% load staticfiles %}
    < link rel="stylesheet" href="{% static "gl_pulltabs/css/foundation.css" %}" / >

    这个STATIC_URL的作用其实是:在settings.py中DEBUG和TEMPLATE_DEBUG都为开的情况下.只要在模版中像上面这样使用,就会自动的在前面加上/static/.例如你想要请求的是/static/app_one/css/mycss.css,那么如果你讲STATIC_URL设置为/static/appone/,那么你只要使用{% static “css/mycss.css” %}就可以了.

    在开发阶段,Django把/static映射到django.contrib.staticfiles这个App。staticfiles自动地从STATICFILES_DIRS、STATIC_ROOT以及各个App的static子目录里面搜索静态文件。一旦布署到开发环境上,settings.py不需要重新编写,只要在Apache的配置文件里面写好映射,/static将会被Apache处理。django.contrib.staticfiles虽然仍然存在,但因为不会接收到以/static/开始的路径,所以将不会产生作用。不必担心Django会使用处理速度变慢。另外,当settings.DEBUG is False的时候,staticfiles将自动关闭

  3. STATICFILES_DIRS
    Here, in settings.py again, you need to explicitly tell django where else to look for static files:
    STATICFILES_DIRS = (
    "/path/to/your/project/yourapp/static/",
    ...
    )

    Can you put your deploy static folder(STATIC_ROOT) path to here, so you can save some disk space? No, you cannot! django won’t allow it.

在django中访问静态文件(js css img)的更多相关文章

  1. 【jsp】怎么在jsp文件中引入静态文件(.js .css)

    如果在jsp文件中引入静态文件比如(.js或.css等等),可以在使用 /项目名称/路径 的方式,但是这种方式如果在修改了项目了名称后就显得比较麻烦了.除了之外还许多方式,比如相对路径等等.一般情况下 ...

  2. django中的静态文件

    静态文件 1.什么是静态文件 在django中静态文件是指那些图片.css样式.js样式.视频.音频等静态资源. 2.为什么要配置静态文件 这些静态文件往往不需要频繁的进行变动,如果我们将这些静态文件 ...

  3. Django中对静态文件的支持(转)

    英文原文:[http://agiliq.com/blog/2013/03/serving-static-files-in-django/] 译文:[http://segmentfault.com/a/ ...

  4. Django 配置访问静态文件

    1.settings.py 首先在 settings 文件中,引用 os 模块: import os   定义根目录: BASE_DIR = os.path.dirname(os.path.dirna ...

  5. spring mvc4 找不到静态文件js/css/html 404

    说明: http://localhost:8080 指向的目录是WEB-INF所在的目录,也就是说请求静态资源时都是从该根目录开始查找.建议将所有静态文件放到和WEB-INF同级的目录下. 以 htt ...

  6. django中关于静态文件的引入(这边是指边主要是jquery和bootstrap

    一.  创建文件夹 首先在项目的根目录中新建一个文件夹,这个文件夹的名称最好以static命名 二.   修改配置 在项目的settings文件中,拉倒最下面,可以看到 STATICFILES_DIR ...

  7. 页面加载异常 清除浏览器静态文件 js css 缓存 js动态加载js css文件,可以配置文件后辍,防止浏览器缓存

    js清除浏览器缓存的几种方法 - 兔老霸夏 - 博客园 https://www.cnblogs.com/Mr-Rocker/p/6031096.html js清除浏览器缓存的几种方法   一.CSS和 ...

  8. Web.Config 对静态文件 js css img 的客户端缓存策略

    <?xml version="1.0" encoding="utf-8"?> <configuration> <system.we ...

  9. Django中使用静态资源/文件

    Django中常需要引用js,css,小图像文件,一般我们把这一类文件称为静态文件,放置在static文件夹中,接下来,对Django中配置静态文件进行下傻瓜式的步骤介绍 在工程目录下新建static ...

随机推荐

  1. eclipse A Java Runtime Environment(JRE)

      eclipse A Java Runtime Environment(JRE) CreateTime--2018年5月13日18点17分 Author:Marydon 1.问题描述 2.问题解析 ...

  2. 如何使用jmeter来实现更大批量的并发的解决方案

    近期在用JMeter进行负载测试的 时候,发现使用单台机器模拟测试超过比如500个进程的并发就有些力不从心或者说不能如实的反应实际情况,在执行的过程中,JMeter自身会自动关闭, 要解决这个问题,则 ...

  3. MassiGra045 简体中文化|打开图片很快

    MassiGra045 简体中文化,是一款对图片的打开预览很高效的工具,据传是日本开发的. 本人之前一直使用,唯一有点缺点就是不能旋转图片. 图片预览 峰回路转: http://pan.baidu.c ...

  4. Windows 消息机制浅析

    1.       Windows 的历史 中国人喜欢以史为鉴,而事实也确实是,如果你能知道一件事情的来龙去脉,往往可以更容易地理解事物为什么会表现为当前这样的现状.所以,我的介绍性开场白通常会以一段历 ...

  5. 两个有序数组求中位数log(m+n)复杂度

    leetcode 第4题 中位数技巧: 对于长度为L的有序数组,它的中位数是(a[ceil((L+1)/2)]+a[floor((L+1)/2)])/2 算法原理: 类似三分法求极值 两个人都前进,谁 ...

  6. Hadoop Archives

    原文地址:http://hadoop.apache.org/docs/r1.0.4/cn/hadoop_archives.html 什么是Hadoop archives? 如何创建archive? 如 ...

  7. Jmeter --- 逻辑控制之if控制器

    一.背景 在实际工作中,当使用Jmeter做性能脚本或者接口脚本时,有可能会遇到需要对不同的条件做不同的操作,基于这种诉求,在Jmeter中可使用if控制器来实现 二.实际操作 逻辑控制器位置: 在线 ...

  8. Linux内核配置解析 - 概述(基于ARM64架构)

    1. 前言 对刚接触Linux kernel的同学来说,遇到的第一个问题就是:我该从哪里入手?. 话说Linux kernel的打开方式是多种多样的:从简单的设备驱动入手:从源代码的目录结构入手:从k ...

  9. Android仿联系人列表分组悬浮列表实现,自己定义PinnedHeaderListView实现

    效果 (关于gif怎么生成的.我先录手机的屏幕得到mp4文件.然后用这个网址:https://cloudconvert.com/mp4-to-gif 进行的mp4转换gif,使用的时候须要又一次选择g ...

  10. Xfire实现webservice各种报错详解

    一.No write method for property {http://vo.aa.com}new in class com.aa.vo.TA 使用xfire的ws调用时,会将对象与xml进行捆 ...