0

Let's say that I've done a good bit of update to my css files on my dev machine (with my browser set to ignore the cache, no always seeing updates right away).

In this question and other places, I've seen the approach of adding a version number to the links:

<link type="text/css" href={% static "/path/mystyles.css?version=2" %} rel="stylesheet">,

which is fine, but on the dev machine, I get an error in the console, and the file isn't found:

GET http://127.0.0.1:8000/path/mystyles.css%3Fversion%3D2 net::ERR_ABORTED 404 (Not Found)

I've read that perhaps collectstatic will fix it on the dev side, but there's an ominous-looking warning:

You have requested to collect static files at the destination
location as specified in your settings:

    /patttthhhhh/static

This will overwrite existing files!
Are you sure you want to do this?

What will happen when I run collectstatic? Where will it get files from to overwrite those?

DeltaG
  • 760
  • 2
  • 9
  • 28

3 Answers3

1

What ended up working easily was changing the format of the version tag:

<link type="text/css" href="{% static '/path/mystyle.css' %}?version=2" rel="stylesheet">

instead of:

<link type="text/css" href={% static "/path/mystyles.css?version=2" %} rel="stylesheet">

Is fine in dev and production.

DeltaG
  • 760
  • 2
  • 9
  • 28
0

The recommended way would be to use something like django-compressor (usage) or the likes to process your CSS into a single file and then bake a hash into the filename or dir. So that way your asset would become /mystyles.beb23fe.css.

Alper
  • 3,424
  • 4
  • 39
  • 45
0

The simplest way I found to do this without having to change the css file every new version was to do it this way using time.time() which will give you a different value every time the page is called. You can do this if your project is somewhat medium or small. I wouldn't do this for a critical project necessarily:

In a file called context_processors.py (I put it in my core app):

import time


def version(request):
    return {"VERSION": str(time.time())}

Then configured it in settings.py:

TEMPLATES = [
    {
        ...
        "OPTIONS": {
            "context_processors": [
                ...
                "core.context_processors.version",
            ],
        },
    },
]

And then in my template I have this:

<link rel="stylesheet" href="{% static 'css/styles.css' %}?version={{VERSION}}">

It seems to work really well in production.

Harlin
  • 1,059
  • 14
  • 18