Reputation: 131
I am just new to django and after making a simple authentication forms for log in and logout.Everything is working perfectly but whenever i want to make some changes to the styles (CSS) it doent work. I have tried after changing the directory of static files but all in vain. Even if i remove all the code from CSS file the page still showing all the style related stuff. kindly suggest me what should i do? below i have attached the image of my all code and directories.
Upvotes: 0
Views: 315
Reputation: 3205
The problem that you are facing is not specific to Django, but you can fix it with Django.
Browsers tend to cache static files such as CSS for better performance and lower data consumption.
One quick fix is to use your browser to clear its cache. The solution is dependent on your browser. However, there are 2 problems with this approach.
To fix these issues once and for all follow these steps:
In your settings.py
add a const and name it something like PROJECT_VERSION
. The value of PROJECT_VERSION
should be the version of your project.
PROJECT_VERSION = "0.0.0.1" # replace the version number with your project version
Create a templates
folder, if you don't have it.
Create a folder named templatetags
inside your templates
folder
Inside the templatetags
, Add an empty file and call it __init__.py
Add another file and call it whatever you want (I call it custom_tags.py
)
Inside the second file, add the following:
from django import template
from django.conf import settings
import uuid
register = template.Library()
@register.simple_tag(name='cache_bust')
def cache_bust():
if settings.DEBUG:
version = uuid.uuid1()
else:
version = settings.PROJECT_VERSION
return '__v__={version}'.format(version=version)
In Your settins.py
, add the following line:
TEMPLATES = [
{
'OPTIONS': {
'libraries':{
'custom_tags': 'templates.templatetags.custom_tags', # Add this line
},
},
},
]
Now, In every template (HTML file) of yours, add this on the top:
{% load custom_tags %}
And as the last step, while referring to any static file (CSS, JS, PNG, etc.), add ?{% cache_bust %}
to the end of the file name. Note that this won't change the content of the file but will force the browser to re-fetch it. For example:
<link rel="stylesheet" type="text/css" href="{% static 'Style.css' %}?{% cache_bust %}" />
Upvotes: 0
Reputation: 3740
Sounds like the CSS file has been cached by your browser. Try clearing your cache and reloading the page. Then you should see if Django is loading in your CSS file or not. If your using Chrome you can do a hard reload with CTRL + Shift + R
Upvotes: 2