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.css
Some explanations:python
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:django
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:session
app_one/static/app_one/cssapp
You are not supposed to put stuff directly under the app's static folder, which might cause name collisions when you collectstatic.ide
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.ui
2. the STATIC_URL in the settings.pythis
This url will appear in your browser:url
(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)idea
# 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.