Question

I am working on writing a Django app for the first time, so bear with me if I'm a little behind on things.

Here is an excerpt from my settings.py:

STATIC_ROOT = os.getcwd().replace('\\','/') + '/static'

STATIC_URL = '/static_files/'

STATICFILES_DIRS = (
    os.getcwd().replace('\\','/') + '/static'
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

Note: I'm hoping that the os.getcwd()... line works. I am pretty sure it's not my problem, but please let me know if this is an issue. It's a placeholder for local dev purposes, so don't worry about it remaining there when I deploy.

My first issue is that template variables don't seem to be working.

An excerpt from my main template file:

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL|default:'/static_files/' }}css/base.css" />

I originally just had {{ STATIC_URL }} in there, but that simply returned nothing, so I tried adding |default:'...'. This did successfully generate the given string in the resulting HTML, but still didn't work, which brings me to my second (and more important, honestly) issue.

I can't get static files to work at all. I have tried several different methods here. I have tried putting absolute and relative paths (in various combinations) in each of the above STATIC_* variables, I have tried using the equivalent MEDIA_URL vars instead, and I have tried putting the following into my urls.py:

urlpatterns = ('',

    # ...

    (r'^static_files/(?P<path>.*)$', 
        'serve', {
            'document_root': '/path/to/django/dir/static_files/',
            'show_indexes': True 
        }
    ),
)

(I grabbed the above snippet from http://www.arthurkoziel.com/2008/09/02/handling-static-files-django/.)

Now I should note that I will hopefully be eventually serving up static files from a parallel Apache process once initial dev is completed, but I would still really like to know what I'm doing wrong. I haven't been able to find a decently comprehensive tutorial on static files online or on StackOverflow.

All help is greatly appreciated.

EDIT: In case it is important, I am on Windows 7 using Python 2.7.

Was it helpful?

Solution

One possibility - STATIC_URL won't be filled out in your templates unless you're passing in a RequestContext.

Make sure you have something like this in your views:

return render_to_response(template, context, context_instance=RequestContext(request))

As of Django 1.3 you can also use the new shortcut:

return render(request, template, context)

Also, make sure you have 'django.core.context_processors.static' in your context processors.


EDIT: Possible answer to the second problem, try changing

STATICFILES_DIRS = (
    os.getcwd().replace('\\','/') + '/static'
)

to

STATICFILES_DIRS = (
    os.getcwd().replace('\\','/') + '/static',
)

EDIT 2: More possible fixes

You can delete STATICFILES_FINDERS. You only have it set to the default, so unless you intend to expand it later on, get rid of it (one less thing to go wrong).

You can also get rid of the urlpatterns entry. Not necessary.

Your STATIC_ROOT is probably incorrect. This should be the place where Django will gather all the static files from across your project (including the directories described in STATICFILES_DIRS) when you move to production.

An example from a typical settings.py of mine:

PROJECT_DIR = os.path.dirname(__file__)

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR, 'static'),
)

STATIC_ROOT = '/var/www/myproject_staticfiles/'

That's it!

If you want to test whether Django is finding and placing your static files correctly, try running

python manage.py collectstatic

In my case, that would go through my project directories, find all the static files, then copy them across to '/var/www/myproject_staticfiles/', where I can host them with apache.

The Django dev server automagically serves static files from within your project folder, so no collect_static is required while in dev. You don't even need to set STATIC_ROOT until you go into production.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top