刚开始参考的是别的文章,后来参考文章《各种 django 静态文件的配置总结》才看到原来没有但是没有注意到版本,折腾了一晚上,浪费了很多很多时间.后来终于知道搜索django1.7访问静态文件.真是傻×.
环境:
python 2.7.3
django 1.7.5
django是不善于处理静态文件这种事情的.这样的工作要交给nginx或者apache这样的服务器.但是在调试时还是要配置一下的
django 1.7.5配置访问静态文件貌似比其他的版本都要简单一些.只需要如下步骤:
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
STATIC_URL = ‘/static/‘
STATICFILES_DIRS = (
os.path.join(BASE_DIR, ‘static/‘),
# ‘/var/www/static‘,
)
{% load staticfiles %} < img src = "{% static "img/test.jpg" %}" alt = "test"/>
或者
{% load staticfiles %} < link rel="stylesheet" href="{% static ‘css/header.css‘ %}" >
注意:
在生产模式中不可设置为True哦,所以生产模式下的配置是不一样的,下面使用nginx来配置静态资源.
补充:
说明一下,有关几个settings中设置的问题 static_root static_url staticfiles_dirs:
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的情况下.可以就不会因为有文件名相同的文件被覆盖掉
# 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将自动关闭。
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.
原文:http://www.cnblogs.com/zknublx/p/5916354.html