Django project structure: how does static folder, STATIC_URL, STATIC_ROOT work
So I've been messing up with Django(1.6+) project setting for quite sometime, this is what i finally settled on. Notice that old django releaes might have a different structure than this.

Some explanations:
1. 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.
This centralized static folder is where django looks for it's files, the individual static folders in each app will NOT be referred to after deployment.
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" ... <!-- BAD -->
... url = "../../static/app_one/img/logo.png" ... <!-- EVEN WORSE -->
You don't want to manually keep track of the STATIC_URL and the hardcoded value and make them synced yourself. (Which is a pretty bad idea). The "../../" way is not acceptable, cuz in production mode, the template and the static files cannot find each other with relative path (it could work with mange.py, but hell no, don't do it!)
Not only that, if you want to switch to CDN or say a remote folder, hardcoded url will be a pain in the ass to reformat later.
In stead, you should do this in your template:
{% load staticfiles %}
<link rel="stylesheet" href="{% static "gl_pulltabs/css/foundation.css" %}"/>
(
More reading on this: {% static %} VS {{ STATIC_URL }}
http://stackoverflow.com/questions/18400315/whats-the-difference-between-using-static-url-and-static
The {% static %} template tag is aware of your STATICFILES_STORAGE, using the STATIC_URL setting is not.
)
When you render the template, it will first interprete the path for you.
Btw way, check if 'django.contrib.staticfiles' is include in settings.py as well, by default it's there, just double check.
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_one',
'app_two',
)
When you run django dev tool (manage.py), you might not be able to see your static files because django is not able to locate them. STATIC_ROOT is ONLY for deployment purposes. So how do you tell django dev tool to look for the static folder inside your app? (Don't ever think about change it back to "../../")
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.
The default dev tool, manage.py is good for dev purposes, it can update the url and template changes immediately (VS where the deployed wsgi might need to restart httpd in order to reflect the changes.)
You want to run it in the background so it won't bother you in the console:
nohup python manage.py runserver 0.0.0.0:8000 &
0.0.0.0 will expose it to outside, if you are running it on a remote server.
Django project structure: how does static folder, STATIC_URL, STATIC_ROOT work的更多相关文章
- [Python] Create a Django project in Pycharm
From: http://blog.csdn.net/u013088062/article/details/50158239 From: http://blog.csdn.net/u013088062 ...
- Start Your Django Project in Nginx with uWsgi
Step 0:Install A,B,C,blabla needed This can be seen in my another article in the blog.click here(una ...
- Apache:To Config The Vhost of Django Project
It is not a good idea to use dev server in Production Environment. Apache or Nginx are good choice.B ...
- Django静态文件的加载以及STATIC_URL、 STATIC_ROOT 、STATICFILES_DIRS的区别
Djangon生产环境静态资源的处理 Django 关闭DEBUG模式后,就相当于是生产环境了. Django框架一旦作为生产环境,它的静态文件访问接口就不应该从Django框架中走,必须在Djang ...
- django project 的快速构建
2003年,堪萨斯(Kansas)州 Lawrence 城中的一个 网络开发小组 ——World Online 小组,为了方便制作维护当地的几个新闻站点(一般要求几天或者几小时内被建立),Adrian ...
- Prepare tasks for django project deployment.md
As we know, there are some boring tasks while deploy Django project, like create db, do migrations a ...
- django 设置静态文件,static
django 设置静态文件,static 一.搜集静态文件 1.1 命令行查看 collectstatic guoguos-MacBook-Pro:mysite guoguo$ python mana ...
- 玩转IDEA项目结构Project Structure,打Jar包、模块/依赖管理全搞定
前言 你好,我是A哥(YourBatman). 如何给Module模块单独增加依赖? 如何知道哪些Module模块用了Spring框架,哪些是web工程? IDEA如何打Jar包?打War包? 熟练的 ...
- .project sturcture和Project Structure 无论是按快捷键或者是从files中打开都不显示
project sturcture和Project Structure 无论是按快捷键或者是从files中打开都不显示 event log中报:IllegalArgumentException:Mul ...
随机推荐
- iOS版本更新的App提交审核流程
App的版本更新估计是在所难免的了!更新App和新的App发布有何不同了?今天我们一起来看看吧!在发布App的时候我们需要通过开发者帐号——(申请)——>发布证书(需要钥匙串对证书签名也叫加密( ...
- iOS 开发之控件快速学习(一)
最近一个朋友想转iOS所以我开始写一些初级iOS学习博客!也希望第一些初学的朋友有所帮助,!好吧进入今天的正题,我们今天主要完成如下界面的显示! 好的一起打开Xcode一下几步我截图说明:
- gdb注意事项
假设我当前有文件test.cpp,要gdb调试,先shell上执行:g++ test.cpp -o test -g 1. gdb调试针对的文件是debug模式的可执行文件. 上面-g参数即加上debu ...
- 如何让WEBAPI 能够进行跨越访问
WebApi域名 http://localhost:11565 当部署好WebApi时,直接通过浏览器进行本地访问 这里是GET请求,此时访问成功 在部署一个Web,域名:http://localho ...
- SQLite常用函数
length(column_name) 取得相应栏位的长度 substr(column_name, start, length) 截取某个栏位相应长度的值
- FTP Proxy Server
本文将在Linux环境下实现一个简单的FTP代理服务器,主要内容涉及FTP主动/被动模式和简单的Socket编程. 1. 主动模式和被动模式 FTP有两种模式,即主动模式(Active Mode)和被 ...
- Uart、SPI和I2C的区别
串口通信:UART.SPI.I2C区别[引用] 1.UART就是两线,一根发送一根接收,可以全双工通信,线数也比较少.数据是异步传输的,对双方的时序要求比较严格,通信速度也不是很快.在多机通信上面 ...
- linux内核学习之三 跟踪分析内核的启动过程
一 前期准备工作 1 搭建环境 1.1下载内核源代码并编译内核 创建目录,并进入该目录: 下载源码: 解压缩,并进入该目录:xz -d linux-3.18.6.tar.xz tar ...
- MySQL Workbench的使用教程 (初级入门版)
MySQL Workbench 是 MySQL AB 最近释放的可视数据库设计工具.这个工具是设计 MySQL 数据库的专用工具. MySQL Workbench 拥有很多的功能和特性:这篇由Djon ...
- ubuntu13.04下建立嵌入式开发平台
linux下建立嵌入式交叉开发平台,需要三个基本部分:编辑工具.交叉工具链以及平台相关库. 一.编辑工具: 一般Linux系统本身都带有编辑工具,比如VI.VIM.gedit等.这里记录的是第三方编辑 ...