feat(base): 在页脚添加代码最后更新时间显示

- 创建自定义模板标签 git_info.py,用于获取 Git 最后提交时间
- 在 base.html 页脚添加代码最后更新时间显示
- 支持显示格式:YYYY-MM-DD HH:MM:SS
This commit is contained in:
2026-03-08 23:12:13 +08:00
parent c123d03922
commit ec21ff68e3
3 changed files with 59 additions and 0 deletions

View File

@@ -1,3 +1,4 @@
{% load git_info %}
<!DOCTYPE html>
<html lang="zh-CN">
<head>
@@ -535,6 +536,10 @@
<i class="bi bi-heart-fill me-2"></i>
2026 家庭日报系统 - 专注于xiaji家庭生活的轻量级日报系统
</p>
<p class="mb-0 mt-2 small text-white-50">
<i class="bi bi-clock-history me-1"></i>
代码最后更新:{% git_last_commit_time %}
</p>
</div>
</footer>

View File

View File

@@ -0,0 +1,54 @@
import subprocess
from django import template
from loguru import logger
register = template.Library()
@register.simple_tag
def git_last_commit_time():
"""
获取 Git 最后提交时间
返回格式YYYY-MM-DD HH:MM:SS
"""
try:
result = subprocess.run(
['git', 'log', '-1', '--format="%cd"', '--date=format:"%Y-%m-%d %H:%M:%S"'],
capture_output=True,
text=True,
check=True,
cwd='.', # 在项目根目录执行
shell=True # Windows 下需要 shell=True 来正确处理引号
)
commit_time = result.stdout.strip().strip('"') # 去除可能的引号
logger.debug(f"获取Git最后提交时间: {commit_time}")
return commit_time
except subprocess.CalledProcessError as e:
logger.error(f"获取Git提交时间失败: {e}")
return "未知"
except FileNotFoundError:
logger.error("Git命令未找到")
return "未知"
except Exception as e:
logger.error(f"获取Git提交时间时发生错误: {e}")
return "未知"
@register.simple_tag
def git_last_commit_hash():
"""
获取 Git 最后提交的短哈希值
"""
try:
result = subprocess.run(
['git', 'log', '-1', '--format=%h'],
capture_output=True,
text=True,
check=True,
cwd='.'
)
commit_hash = result.stdout.strip()
return commit_hash
except Exception as e:
logger.error(f"获取Git提交哈希失败: {e}")
return "unknown"