Am I using static files "wrong"?

58 views
Skip to first unread message

Micah Carrick

unread,
Oct 4, 2012, 10:58:18 AM10/4/12
to django...@googlegroups.com
Regarding Django 1.4...

I recently came across a post that suggests that the way I'm managing static files is "wrong". I wanted to make sure I'm clear on things.

In development, static files are in the "static" folders both within apps at the project level. STATIC_URL = '/static/'

When I deploy a project, the collectstatic command copies *from* these directories to a public folder on my static asset domain. STATIC_URL = 'http://static.foobar.com/', STATIC_ROOT = "/var/www/static.foobar.com"

Therefore, the "static" folder in my project directory is added to STATICFILES_DIRS. Is that wrong? It was suggested that I'm supposed to be collecting static files *to* the "static" folder--but that seems strange to me as I don't want to collect static assets into my version controlled source tree.

Secondly, I've heard that we should be using the static template tag and not the STATIC_URL context variable. I see the advantages of using the template tag, but, it's not wrong to use STATIC_URL right? It's not deprecated or anything?

Cheers,
- Micah


Chris Lawlor

unread,
Oct 4, 2012, 12:41:13 PM10/4/12
to django...@googlegroups.com
Micah,

Here's a summary of how staticfiles works:

1. You place your static files, which are under version control, in app/static folders, or any directory defined in STATICFILES_DIRS. This is configurable, and works very similarly to how templates are placed in a project. The important thing to note is that your 'source' static files can be scattered among several directories, all of which are under version control.
2. You define STATIC_ROOT, which is the absolute path to some directory on your filesystem where you want the static files to be collected to. STATIC_ROOT would not be under version control, although some developers like to add an empty '_static' folder to version control so you don't have to manually create the STATIC_ROOT folder when deploying the project to a new machine.
3. Running 'collectstatic' collects all of your static files, and copies them to STATIC_ROOT.
4. You configure your production webserver to serve '/static/' (or whatever STATIC_URL is set to) from STATIC_ROOT. If you add staticfiles_urlpatterns to your URL config, the development server will serve your static files if DEBUG=True.


The template context processor django.core.context_processors.static, which provides STATIC_URL, is not deprecated. Feel free to use either that or the template tag.


Hope that helps,

Chris
Reply all
Reply to author
Forward
0 new messages