Vanice Leung
Vanice Leung

Reputation: 344

CSS in django doesn't update

The problem that I have is not I fail to load css file, is that I can't update them. The first few times I edit after I have just created my css files, the changes were working. But after a while, when I try to change for example: the color, it is not changing. The colour on the web is not changing and I click to see the source file, it is using the unchanged version. I have also tried to use python manage.py collectstatic but still can't update the file.

# in settings.py 
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
# in main urls.py 
if settings.DEBUG: 
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

Upvotes: 0

Views: 528

Answers (2)

Vanice Leung
Vanice Leung

Reputation: 344

On mac, press shift then reload the page. It's working.

Upvotes: 0

KJTHoward
KJTHoward

Reputation: 876

I had this problem the other day... My issue was because the css was being cached and the cached version was being used rather than the new version.

Check what the status code of the CSS being loaded is, if you're just running a dev server with "manage.py runserver" then the status code is the first number after the "GET " output.

If the status code for your css is 304 then it is being cached. Alternatively in Chrome and Firefox (probably other browsers too but they're what I use) open then Dev tools (Ctrl+Shift+I) and go to the "Network" tab. When you reload the page if the size of your css is listed as "(memory cache)" then a cached version is being used.

To reload the page loading the new css you need to reload the page without using the cache, for Chrome and Firefox you can do Ctrl+Shift+R, the status code for your CSS should then show as 200.

To make it easier, what I did (in Chrome), is open then Dev tools (Ctrl+Shift+I) and click the "Network" tab and check the "Disable cache", this forces the site to load all static files (including the css) from the server everytime rather than using the cached version.

Upvotes: 1

Related Questions