Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django's CachedStaticFilesStorage not hashing file urls

I wanted to enabling versioning to some of my javascript and css files as I was getting caching problems when working on the site. I read about CachedStaticFilesStorage in Django 1.6 and it seemed perfect. I modified my settings.py to the following settings:

STATIC_ROOT = 'staticfiles'

STATIC_URL = ''

# Additional locations of static files
STATICFILES_DIRS = (
)

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

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.CachedStaticFilesStorage'

As a test I then rewrote the most problematic of the css tags to see if it would start hashing the file path. I instead got a 500 error whenever I try and load the page.

Any ideas where I went wrong? Is there an additional step I missed?

The entry in the template:

{% block cssfiles %}
{% load static%}
<link href="{% static "/static/css/mapmaker.css" %}" media="screen">
{% endblock %}
like image 591
ChargerIIC Avatar asked Mar 02 '14 17:03

ChargerIIC


People also ask

What does Collectstatic do in Django?

collectstatic. Collects the static files into STATIC_ROOT . Duplicate file names are by default resolved in a similar way to how template resolution works: the file that is first found in one of the specified locations will be used. If you're confused, the findstatic command can help show you which files are found.

How does Django find static files?

Using the collectstatic command, Django looks for all static files in your apps and collects them wherever you told it to, i.e. the STATIC_ROOT . In our case, we are telling Django that when we run python manage.py collectstatic , gather all static files into a folder called staticfiles in our project root directory.

What is static root?

STATIC_ROOT is the single root directory from where the Django application will serve the static files in production. While deployment you would typically want to serve the static files from the conventional /var/www/example.com.

How can you set up static files in Django?

Configuring static filesMake sure that django.contrib.staticfiles is included in your INSTALLED_APPS . In your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE . Store your static files in a folder called static in your app.


1 Answers

Very tricky... If you read the docs carefully, you will learn:

... use the staticfiles static template tag to refer to your static files in your templates ...

So instead of:

{% load static %}

Use

{% load staticfiles %}
like image 126
Udi Avatar answered Oct 22 '22 14:10

Udi