Django应用程序不更新浏览器中的静态文件

时间:2021-10-24 02:10:43

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.

当我的Django站点在带有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):

settings.py(相关部分):

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.

我使用python manage.py collectstatic来收集我的应用程序的静态文件和第三方应用程序(django-autocomplete-light)到STATIC_ROOT。它们存在于STATIC_ROOT中。然后我用runserver提供了我的网站。静态文件随站点一起加载,其路径匹配回STATIC_ROOT。当我编辑静态文件(即自动完成应用程序的select2.css)时,更改显示在STATIC_ROOT中。但是,它们不会显示在浏览器中。

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.

我已经尝试清除缓存,在各种浏览器中提供服务,杀死/重启runserver,杀死/重新启动我的文本编辑器,然后重新运行collectstatic。这些尝试都没有奏效 - 静态文件在加载网站时根本不会更新。

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.

如何在浏览器中更新静态文件?我想我可能要么在settings.py中缺少设置,要么{{formset.media}}可能存在问题。不过我很茫然。

1 个解决方案

#1


0  

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

记得添加STATICFILES_DIRS,Django需要url到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 %}

并且看看这个{%load staticfiles%}和{%load static%}之间的区别是什么?

#1


0  

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

记得添加STATICFILES_DIRS,Django需要url到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 %}

并且看看这个{%load staticfiles%}和{%load static%}之间的区别是什么?