Django app not updating static files in browser

1.8k views Asked by At

The static files from third-party apps are not being updated in my Django site when it is served up in a browser with runserver.

Here is my file structure (with many more static files):

- mysite
  - mysite
    - settings.py
    - wsgi.py
    . . .
  - myapp
    - templates
      - base.html
      - myapp.html
    - models.py
    - forms.py
    . . . 
  - static
    - MyApp
      - mystyle.css
    - autocomplete-light
      - select2.css

base.html:

{% load static %}
<!DOCTYPE html>
<html>
<head>
  <title>
    {% block title %}Portal{% endblock title %}
  </title>
  {% block stylesheets %}
    <link rel="stylesheet" type="text/css" href="{% static 'myapp/mystyle.css' %}?{% now "U" %}"/>
  {% endblock stylesheets %}
</head>
<body>
  {% block content %}
  {% endblock content %}
</body>
<footer>
  {% block footer %}
  {% endblock footer %}
</footer>
</html>

myapp.html:

{% extends "myapp/base.html" %}
{% load static %}
{% load crispy_forms_tags %}
{% block title %}My App{% endblock title %}
{% block stylesheets %}
  {{ block.super }}
{% endblock stylesheets %}
{% block content %}
  {% crispy formset helper %}
{% endblock content %}
{% block footer %}
<script type="text/javascript" src="{% static 'admin/js/vendor/jquery/jquery.js' %}?{% now "U" %}"></script>
{{ formset.media }}
{% endblock %}

settings.py (relevant piece):

STATIC_ROOT = 'C:/Users/username/mysite/static/'
STATIC_URL = '/static/'

I used python manage.py collectstatic to collect the static files for my app and the third party app (django-autocomplete-light) into the STATIC_ROOT. They exist in the STATIC_ROOT. I then served up my site with runserver. The static files are loaded with the site and their paths match back to the STATIC_ROOT. When I edit the static files (i.e. select2.css for the autocomplete app), the changes show up in STATIC_ROOT. However, they do not show up in the browser.

I have tried clearing my cache, serving the site in various browsers, killing/restarting runserver, killing/restarting my text editor, and rerunning collectstatic. None of these attempts have worked - the static files simply will not update when the site is loaded.

How do I get the static files to update in the browser? I think I might be either missing a setting in settings.py, or it may be an issue with {{ formset.media }}. I am at a loss, though.

1

There are 1 answers

0
Mauricio Cortazar On

Remember add STATICFILES_DIRS, Django need the url to the staticfiles:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
    '/var/www/static/', # If you have more staticfiles this will be the order
)

and also take a look to this What is the difference between {% load staticfiles %} and {% load static %}