Rubén Jiménez
Rubén Jiménez

Reputation: 1845

STATIC files are not showed in Django templates

I'm trying to show images/js/css in my Django templates with {{ STATIC_URL }} label. In theory, this works because in browser, when i see the printed code the path to requested file is something like "/static/images/blabla.jpg". But the file is not showed, so i guess the problem is in Django project.

My settings looks like this:

STATIC_ROOT = '/absolute/path/to/static'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    '/absolute/path/to/static'
)

In my urls.py:

url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),

But i don't know why all files in static directory are not displayed in templates.

Any tip or advice?

Thanks mates.

Upvotes: 1

Views: 240

Answers (1)

jpic
jpic

Reputation: 33420

First, it looks like there is a mistake in your settings. You should understand the django.contrib.staticfiles collectstatic command:

Collects the static files into STATIC_ROOT. [...] Files are searched by using the enabled finders. The default is to look in all locations defined in STATICFILES_DIRS and in the 'static' directory of apps specified by the INSTALLED_APPS setting.

So, it seems like a mistake that STATICFILES_DIRS contains STATIC_ROOT, since the contents of STATICFILES_DIR is ment to be copied into STATIC_ROOT by collectstatic.

The url is not necessary. You should either:

When in doubt, the findstatic command may also prove useful.

django.contrib.staticfiles might look a little confusing at first, but when you understand it fully you love it <3

Upvotes: 2

Related Questions