0

I've been trying to follow every single tutorial and thread there is, i jast cant get it togheter.

Sorry, but please exlpain it to me as if i was three years old..

What i have: a CSS that i'd like use, styling a template.

How to get there: MD: project/app/static/

place style.css there

settings.py:

STATIC_ROOT = '/project/app/static/'

STATIC_URL = '/static/'

(Should'nt this at least give me the string '/static/', when i try printing {{ STATIC_URL }} in the template..?)

template:
<LINK REL=StyleSheet HREF="{{ STATIC_URL }}/style.css" TYPE="text/css" MEDIA=screen>

Seems to me, the next logical thing would be to create an /static/-url aswell?

I'm really lost here..

BSG
  • 1,382
  • 4
  • 14
  • 25
  • You need to have the appropriate staticfiles context processor installed. – Marcin Mar 26 '12 at 12:56
  • 1
    Don't follow every tutorial that there is, you only need the one and onle django documentation which is found at the django website. Folow only that tutorial, for 1.3, the others have confused you. – rapadura Mar 26 '12 at 12:58
  • Installed apps: 'django.contrib.staticfiles', Are there any more that i'll need? – BSG Mar 26 '12 at 13:04

1 Answers1

2

Django's staticfiles system is about two things:

  1. Combining static files from a number of (app-specific) directories into a single place from which they can be served

  2. Serving those files over HTTP

You want #1 if you have a number of static files located in different directories. You want #2 if you want to see those files while you are using the development server. In production, you usually skip #2, because the purpose of #1 is to put the files somewhere that your web server can access them.

Organizing and collecting static files

With that said, here's how you get #1:

settings.py:

# This is a filesystem path
STATIC_ROOT = '/path/to/my/project/collected-static-files/'

# This is a URL prefix
STATIC_URL = '/static/'

INSTALLED_APPS = (
    ...
    'django.contrib.staticfiles',
    ...
)
TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'django.core.context_processors.static',
    ...
)

At this point, you can do a few things:

  1. You put your static files in an '/static/' directory under each of your apps. That's where django will look for them. There is an additional setting, STATIC_DIRS, that you can use if you have static files that are outside of your apps.

  2. You can run manage.py collectstatic to have Django go through all of your apps, finding all of the static files, and copying them into /path/to/my/project/collected-static-files (That's why that setting is a filesystem path.)

  3. You can use {{ STATIC_URL }} in templates to access the files.

Serving static files over HTTP

In development mode, under the dev server, Django will automatically serve files out of the STATIC_ROOT directory.

Basically, Django will add a URL handler for URLs of the form

/static/some-dir/some-file-name

And serve content from your hard drive, from the filesystem path

/path/to/my/project/collected-static-files/some-dir/some-file-name

In production, this won't happen automatically, so you will have to configure your web server to do the same thing. (It's just not a good idea to run all of your static files through Django's full processing pipeline. The web server can do it much more efficiently)

Ian Clelland
  • 43,011
  • 8
  • 86
  • 87
  • I still cant get anything out from {{ STATIC_URL }}, the STATIC_ROOT should point to a existing folder on my drive, right? so 'C:/blablbalba/static_stuff' should be ok? – BSG Mar 26 '12 at 20:01
  • I've got all the static files gruoped togheter, using manage.py collectstatic, so at least they are where they are supposed to be.. – BSG Mar 26 '12 at 20:09
  • Did you add `'django.core.context_processors.static'` to your `TEMPLATE_CONTEXT_PROCESSORS` in settings.py? – Ian Clelland Mar 27 '12 at 17:41
  • Thing is, i did'ny even have a TEMPLATE_CONTEXT_PROCESSORS in the first place, but yes. (After adding it..) – BSG Mar 27 '12 at 21:17
  • Finally i've got it to work. Although not the right way, but by using MEDIA_ROOT instead. Which will have to do, for this assignment at least. Thank you for your efforts. – BSG Mar 29 '12 at 23:44
  • Every setting in `settings.py` has a default, even `TEMPLATE_CONTEXT_PROCESORS` -- the default value is documented at https://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors – Ian Clelland Apr 07 '12 at 15:38