当前位置: 首页 > news >正文

每天40分玩转Django:Django静态文件

Django静态文件

一、今日学习内容概述

学习模块重要程度主要内容
静态文件配置⭐⭐⭐⭐⭐基础设置、路径配置
CDN集成⭐⭐⭐⭐⭐CDN配置、资源优化
静态文件处理⭐⭐⭐⭐压缩、版本控制
部署优化⭐⭐⭐⭐性能优化、缓存策略

二、基础配置

# settings.py
import os# 静态文件配置
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),
]# 静态文件查找器
STATICFILES_FINDERS = ['django.contrib.staticfiles.finders.FileSystemFinder','django.contrib.staticfiles.finders.AppDirectoriesFinder',
]# CDN 配置
CDN_DOMAIN = 'https://cdn.example.com'
USE_CDN = True# 压缩配置
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'

三、项目结构示例

myproject/
├── manage.py
├── myproject/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── static/
│   ├── css/
│   │   ├── main.css
│   │   └── vendor/
│   ├── js/
│   │   ├── main.js
│   │   └── vendor/
│   └── images/
└── templates/├── base.html└── includes/

四、静态文件管理器

# storage.py
from django.contrib.staticfiles.storage import StaticFilesStorage
from django.conf import settings
import os
import hashlibclass CustomStaticStorage(StaticFilesStorage):"""自定义静态文件存储"""def __init__(self, *args, **kwargs):super().__init__(*args, **kwargs)self.prefix = settings.STATIC_URL.rstrip('/')def url(self, name):"""生成文件URL"""url = super().url(name)if settings.USE_CDN:return f"{settings.CDN_DOMAIN}{url}"return urldef hashed_name(self, name, content=None, filename=None):"""生成带哈希值的文件名"""if content is None:return namemd5 = hashlib.md5()for chunk in content.chunks():md5.update(chunk)hash_value = md5.hexdigest()[:12]name_parts = name.split('.')name_parts.insert(-1, hash_value)return '.'.join(name_parts)

五、模板使用示例

<!-- templates/base.html -->
{% load static %}
<!DOCTYPE html>
<html>
<head><title>{% block title %}{% endblock %}</title><!-- CSS 文件 --><link rel="stylesheet" href="{% static 'css/vendor/bootstrap.min.css' %}"><link rel="stylesheet" href="{% static 'css/main.css' %}"><!-- 自定义CDN引用 -->{% if settings.USE_CDN %}<link rel="preconnect" href="{{ settings.CDN_DOMAIN }}">{% endif %}
</head>
<body><nav class="navbar"><img src="{% static 'images/logo.png' %}" alt="Logo"><!-- 导航内容 --></nav><main>{% block content %}{% endblock %}</main><!-- JavaScript 文件 --><script src="{% static 'js/vendor/jquery.min.js' %}"></script><script src="{% static 'js/vendor/bootstrap.bundle.min.js' %}"></script><script src="{% static 'js/main.js' %}"></script>
</body>
</html>

六、静态文件处理流程图

在这里插入图片描述

七、CDN配置和优化

# cdn.py
from django.core.files.storage import get_storage_class
from django.conf import settings
import requestsclass CDNStorage:"""CDN存储管理器"""def __init__(self):self.storage = get_storage_class()()self.cdn_domain = settings.CDN_DOMAINdef sync_file(self, path):"""同步文件到CDN"""try:with self.storage.open(path) as f:response = requests.put(f"{self.cdn_domain}/{path}",data=f.read(),headers={'Content-Type': self.storage.mime_type(path),'Cache-Control': 'public, max-age=31536000'})return response.status_code == 200except Exception as e:print(f"CDN同步失败: {str(e)}")return Falsedef purge_file(self, path):"""清除CDN缓存"""try:response = requests.delete(f"{self.cdn_domain}/purge/{path}",headers={'Authorization': f'Bearer {settings.CDN_API_KEY}'})return response.status_code == 200except Exception as e:print(f"缓存清除失败: {str(e)}")return False

八、静态文件压缩

# compressor.py
from django.contrib.staticfiles.storage import CompressedManifestStaticFilesStorage
import subprocessclass CustomCompressedStorage(CompressedManifestStaticFilesStorage):"""自定义压缩存储"""def post_process(self, paths, dry_run=False, **options):"""处理文件后进行压缩"""for path in paths:if path.endswith(('.css', '.js')):full_path = self.path(path)# CSS压缩if path.endswith('.css'):subprocess.run(['cleancss', '-o', full_path, full_path])# JS压缩if path.endswith('.js'):subprocess.run(['uglifyjs', full_path, '-o', full_path])return super().post_process(paths, dry_run, **options)# 压缩命令
from django.core.management.base import BaseCommandclass Command(BaseCommand):help = '压缩静态文件'def handle(self, *args, **options):storage = CustomCompressedStorage()storage.collect()

九、性能优化建议

  1. 文件合并
# utils.py
def combine_files(file_list, output_path):"""合并多个文件"""with open(output_path, 'wb') as output:for file_path in file_list:with open(file_path, 'rb') as input_file:output.write(input_file.read())output.write(b'\n')
  1. 缓存配置
# settings.py
CACHES = {'default': {'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache','LOCATION': '127.0.0.1:11211',}
}# 静态文件缓存设置
STATICFILES_CACHE_TIMEOUT = 60 * 60 * 24 * 30  # 30天
  1. 图片优化
# image_optimizer.py
from PIL import Image
import osdef optimize_image(input_path, output_path=None, quality=85):"""优化图片质量和大小"""if output_path is None:output_path = input_pathwith Image.open(input_path) as img:# 保存优化后的图片img.save(output_path,quality=quality,optimize=True)
  1. 版本控制
# context_processors.py
from django.conf import settingsdef static_version(request):"""添加静态文件版本号"""return {'STATIC_VERSION': getattr(settings, 'STATIC_VERSION', '1.0.0')}

十、部署注意事项

  1. 收集静态文件
python manage.py collectstatic --noinput
  1. Nginx配置
# 静态文件服务
location /static/ {alias /path/to/staticfiles/;expires 30d;add_header Cache-Control "public, no-transform";
}
  1. 监控和日志
# middleware.py
class StaticFileMonitorMiddleware:def __init__(self, get_response):self.get_response = get_responsedef __call__(self, request):if request.path.startswith(settings.STATIC_URL):# 记录静态文件访问logger.info(f"Static file accessed: {request.path}")return self.get_response(request)

通过本章学习,你应该能够:

  1. 配置Django静态文件系统
  2. 集成和使用CDN
  3. 实现静态文件优化
  4. 管理文件版本和缓存

怎么样今天的内容还满意吗?再次感谢朋友们的观看,关注GZH:凡人的AI工具箱,回复666,送您价值199的AI大礼包。最后,祝您早日实现财务自由,还请给个赞,谢谢!

http://www.lryc.cn/news/509990.html

相关文章:

  • Linux 线程池
  • windows使用zip包安装MySQL
  • 深度学习实战之超分辨率算法(tensorflow)——ESPCN
  • Android unitTest 单元测试用例编写(初始)
  • C++简明教程(10)(初识类)
  • 光谱相机的工作原理
  • 【Linux进程】基于管道实现进程池
  • 软件测试之单元测试
  • vscode+编程AI配置、使用说明
  • 007-spring-bean的相关配置(重要)
  • 【唐叔学算法】第19天:交换排序-冒泡排序与快速排序的深度解析及Java实现
  • 合并 Python 中的字典
  • 使用Python实现自动化文档生成工具:提升文档编写效率的利器
  • uniapp使用live-pusher实现模拟人脸识别效果
  • 【JavaSE】【网络原理】初识网络
  • 鸿蒙之路的坑
  • Python生日祝福烟花
  • Ubuntu环境 nginx.conf详解(二)
  • shardingsphere分库分表项目实践4-sql解析sql改写
  • mysql数据库中,一棵3层的B+树,假如数据节点大小是1k,那这棵B+可以存多少条记录(2100万的由来)
  • Git 操作全解:从基础命令到高级操作的实用指南
  • 华院计算参与项目再次被《新闻联播》报道
  • 从一次线上故障聊聊接口自动化测试
  • Element-ui的使用教程 基于HBuilder X
  • Chapter 03 复合数据类型-1
  • 【Python知识】Python面向对象编程知识
  • CSharp: Oracle Stored Procedure query table
  • “协同过滤技术实战”:网上书城系统的设计与实现
  • Dhatim FastExcel 读写 Excel 文件
  • YOLO11全解析:从原理到实战,全流程体验下一代目标检测