37

I have this code to refer to my CSS script in my base.html for my Django project:

<link href="{% static 'css/project.css' %}" rel="stylesheet">

The problem is that whenever I add or remove styling from project.css, it doesn’t update when I run the server.

I know this happens because every time the page loads the browser cache will think it’s seen the file before and reload the cached version from disk. I also know a solution is to change the CSS filename every time I make an update.

Is there an easier way to reload the CSS file every time I refresh my browser?

ib.
  • 27,830
  • 11
  • 80
  • 100
Kartikeya Sharma
  • 463
  • 2
  • 5
  • 8

11 Answers11

106

You can bypass the cache using ctrl + F5

for detailed reference: https://en.wikipedia.org/wiki/Wikipedia:Bypass_your_cache

Abhijith Konnayil
  • 4,067
  • 6
  • 23
  • 49
18

You have to click on Ctrl + f5 every time to update your CSS, which isn't dope. You can actually do something like this:

<link rel="stylesheet" href="/static/css/mycss.css?{% now "U" %}"/>

The {% now "U" %} will generate a random timestamp which updates your CSS every time you reload the page. I hope this works.

For better understanding check out: click here

Xamuel San
  • 207
  • 3
  • 7
6

If you are on mac and using safari, you can press shift then press the reload key.

Vanice Leung
  • 304
  • 1
  • 3
  • 10
2

ctrl+f5 for Chrome and Windows helped.

2

Bypass your cache — Simple instructions

In most Windows and Linux browsers:

Hold down Ctrl and press F5.

In Apple Safari:

Hold down ⇧ Shift and click the Reload toolbar button. In Chrome and Firefox for Mac:

Hold down both ⌘ Cmd+⇧ Shift and press R.

know more

MD SHAYON
  • 7,001
  • 45
  • 38
2

It happens during development (when DEBUG is set to True in settings.py) if you change the static files under the directory set STATIC_ROOT, instead of the static directories under the particular app.

In other words, when working locally (on localhost) and DEBUG is set to True, do not forget to change the static files served under your app directory.

Luke
  • 965
  • 8
  • 21
1

In addition to the answer of Xamuel San, if you are using Jinja to link your stylesheet, then you can try the following:

<link rel="stylesheet" type="text/css" href='{% static '/css/main.css' %}?{% now "U" %}'>

It perfectly worked on Chrome and Firefox. You can also use crtl+f5 to do the same but manually.

1

In chrome you can press Ctrl + F5. If this does not work restart apache

Darwin
  • 1,695
  • 1
  • 19
  • 29
0

i prefer to temporarily disable the browser cache.

right click => inspect => network

[]Disable cache

0

You can try this:

specify type attribute in the link tag/field.

<link rel="stylesheet" type="text/css" href="{% static '/css/Style.css' %}"/>
Ajay Lingayat
  • 1,465
  • 1
  • 9
  • 25
0

Most browsers allow you to disable http caching to some degree. In Mozilla, if you're using the Web Developer tools, there's a setting to do this.

Mozilla Web Dev Tool Settings

Austin A
  • 2,990
  • 6
  • 27
  • 42