动态生成新的静态url以使浏览器的缓存失效
django-static-url的Python项目详细描述
Authors: | Resulto Developpement Web Inc. |
---|---|
Version: | 0.1.0 |
默认情况下,静态资源的uri在部署和 每个项目都有一个解决方案来使浏览器的 隐藏物。django static url添加可以在 django设置文件,在视图和模板中。我们的想法是创造一个新的 /static/url每次重新加载dev服务器,每次 代码部署在生产环境中。
它是如何工作的?
在最简单的形式中,django静态url将基于 当前时间并在静态URL前缀之间插入此哈希(例如, /static/)和静态文件路径。每次 devserver被重新加载,因此对静态文件的任何更改都应该由 无需清空或绕过缓存的浏览器。
在生产环境中,您可能不希望每次进程更改url 重新加载,以便您可以提供其访问时间为 用于计算哈希值。例如,我们给出了uwsgi配置的路径 因为每次部署只访问一次。
未来将提供更多策略,以适应各种情况 (例如,不共享文件的负载平衡应用服务器)。
django静态url假设您知道如何配置生产web 服务器。大概,您正在使用nginx并在 绕过python web服务器来提供静态文件。我们提供了一个例子 以下安装说明中的nginx定位块。
要求
django静态url与python 3.4+一起工作。它需要django 1.8+
安装
安装库
pip install django-static-url
在django设置(开发)中添加此片段
# The prefix of the static URL. STATIC_ROOT_URL = "/static/" # Will change the URL everytime the settings/server is reloaded. from django_static_url_helper import url_helper STATIC_URL = url_helper.get_static_url_now( STATIC_ROOT_URL, True, SECRET_KEY)
备选方案2:在django设置(生产)中添加此片段
# The prefix of the static URL. STATIC_ROOT_URL = "/static/" # Will change the URL everything the SOME_IMPORTANT_FILE_PATH is touched # (url generated based on access time). from django_static_url_helper import url_helper STATIC_URL = url_helper.get_static_url_file( STATIC_ROOT_URL, SOME_IMPORTANT_FILE_PATH, True, SECRET_KEY)
将此添加到您的url文件(开发)
from django.conf import settings from django_static_url_helper.django_url_helper import staticfiles_dynamicurlpatterns # Define your urlpatterns here urlpatterns = ... if settings.DEBUG: urlpatterns += staticfiles_dynamicurlpatterns(settings.STATIC_ROOT_URL)
将此添加到nginx配置文件(生产)中
# regex that captures all requests made to # /static(/HASH_CODE)/STATIC_FILE_PATH location ~ ^/static/([a-f0-9-]+/)?(.*)$ { # Set static file expiration expires 7d; alias /path/to/djangoproject/static/$2; }
许可证
此软件在新的bsd许可证下获得许可。参见许可证文件 在存储库中获取完整的许可证文本。
正在签名GPG密钥
以下GPG密钥可用于签署标记和发布文件:
- 巴塞勒米·达格奈斯:76320A1B901510C4