STATIC_ROOT confusion

55 views
Skip to first unread message

Roy Smith

unread,
Oct 7, 2011, 10:13:18 PM10/7/11
to Django users
I'm mystified why django (under runserver) is not finding my static
files. I'm running django-1.3.1. The relevant parts of my
settings.py file are:

STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
STATIC_URL = '/static/'

I have a file in my static directory, a file named foo. If I do a GET
on http://localhost:8000/static/foo, I get an 404 with the message,
"'foo' could not be found".

I have commented django.contrib.staticfiles out of my settings.py
file. For now, I just want to drop my static files manually into my
static directory.

Xavier Ordoquy

unread,
Oct 8, 2011, 1:35:47 AM10/8/11
to django...@googlegroups.com
HI,

You don't need STATIC_ROOT while using your dev server.
If you have a look https://docs.djangoproject.com/en/1.3/howto/static-files/#basic-usage it isn't even mentionned in the basic usage section.

Regards,
Xavier.

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

Markus Gattol

unread,
Oct 8, 2011, 9:16:17 AM10/8/11
to django...@googlegroups.com
if your STATICFILES_DIRS tuple contains '/a/b/static/img' then it will work if you put foo.jpg into /a/b/static/img/foo.jpg and have STATIC_URL = '/static/'. STATIC_ROOT doesn't have something to do with that, it's used to collect static stuff like CSS files from your apps etc. I think if people would set STATIC_ROOT = os.path.join(PROJECT_DIR, 'static_root') it would be less confusing to them anyway. 

Markus Gattol

unread,
Oct 8, 2011, 9:27:46 AM10/8/11
to django...@googlegroups.com
Here's what I think is semantically good distinction:

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))   (that's the dir containing your settings.py, that dir is usually one dir below your virtualenv, I simply name it "pr". You can then use PROJECT_ROOT to reference such as:

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static_root')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static/css'),
    os.path.join(PROJECT_ROOT, 'static/img'),
    os.path.join(PROJECT_ROOT, 'static/js'),
)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

PROJECT_ROOT, "pr" on the filesystem is what you get when you run django-admin.py startproject pr after you run mkvirtualenv foo.com (foo.com would then be the root of your virtualenv; some also call it SITE_ROOT in Django context because they keep web server config, Sass files, etc. there). Here's the dir structure I came to enjoy, also because it maps nicely to the settings posted above:

sa@sub:~/0/2/sr$ type tad2; tad2
tad2 is aliased to `tree  --charset ascii -ad -L 2  -I \.git*\|*\.\~*\|*\.pyc'
.
|-- bin
|-- cache
|-- gems
|   `-- bin
|-- include
|-- lib
|-- log
|-- pid
|-- pkg
|-- pr
|   |-- apps
|   |-- config
|   |-- docs
|   |-- etc -> etcs/development/
|   |-- etcs
|   |-- fixtures
|   |-- formats
|   |-- libs
|   |-- media
|   |-- media_root
|   |-- static
|   |-- static_root
|   |-- templates
|   `-- tests
|-- sass
|-- share
|-- sock
`-- tmp

28 directories
sa@sub:~/0/2/sr$ 


Xavier Ordoquy

unread,
Oct 8, 2011, 11:07:34 AM10/8/11
to django...@googlegroups.com

Le 8 oct. 2011 à 15:27, Markus Gattol a écrit :

Here's what I think is semantically good distinction:

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))   (that's the dir containing your settings.py, that dir is usually one dir below your virtualenv, I simply name it "pr". You can then use PROJECT_ROOT to reference such as:

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static_root')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static/css'),
    os.path.join(PROJECT_ROOT, 'static/img'),
    os.path.join(PROJECT_ROOT, 'static/js'),
)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

PROJECT_ROOT, "pr" on the filesystem is what you get when you run django-admin.py startproject pr after you run mkvirtualenv foo.com (foo.com would then be the root of your virtualenv; some also call it SITE_ROOT in Django context because they keep web server config, Sass files, etc. there). Here's the dir structure I came to enjoy, also because it maps nicely to the settings posted above:

Hi,

Your STATICFILES_DIRS should not be set to static.
Create a new theme directory, put your files there and update your STATICFILES_DIRS.

Regards,
Xavier.

Markus Gattol

unread,
Oct 8, 2011, 11:23:59 AM10/8/11
to django...@googlegroups.com

Your STATICFILES_DIRS should not be set to static.
Create a new theme directory, put your files there and update your STATICFILES_DIRS.

Yeah, read that post and I disagree. Introducing yet another name on the filesystem (theme) certainly isn't helping people (this thread and many others show there's enough confusion already). It makes more sense to have a 1:1 mapping between variable naming in settings.py and directories on the filesystem e.g. STATIC_ROOT to static_root, STATIC_DIRS to static/<foo>. Plus, Django is not Joomla or Plone or... so thinking in terms of "themes" is semantically wrong as it makes you think of Django as a CMS (one layer to high).

Xavier Ordoquy

unread,
Oct 8, 2011, 11:35:00 AM10/8/11
to django...@googlegroups.com

Le 8 oct. 2011 à 17:23, Markus Gattol a écrit :

Yeah, read that post and I disagree. Introducing yet another name on the filesystem (theme) certainly isn't helping people (this thread and many others show there's enough confusion already). It makes more sense to have a 1:1 mapping between variable naming in settings.py and directories on the filesystem e.g. STATIC_ROOT to static_root, STATIC_DIRS to static/<foo>. Plus, Django is not Joomla or Plone or... so thinking in terms of "themes" is semantically wrong as it makes you think of Django as a CMS (one layer to high).


Then forget about STATIC and stick to MEDIA.
I feel the STATIC way is much better for production because it keeps the real static data away from more dynamic data (uploaded or computed files) but it requires some comprehension of what one does.

Regards,
Xavier,
Linovia.
Reply all
Reply to author
Forward
0 new messages