From ec21ff68e3eb53cdbf7e52811466f1d1967b7eae Mon Sep 17 00:00:00 2001 From: xiaji Date: Sun, 8 Mar 2026 23:12:13 +0800 Subject: [PATCH] =?UTF-8?q?feat(base):=20=E5=9C=A8=E9=A1=B5=E8=84=9A?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BB=A3=E7=A0=81=E6=9C=80=E5=90=8E=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=97=B6=E9=97=B4=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 创建自定义模板标签 git_info.py,用于获取 Git 最后提交时间 - 在 base.html 页脚添加代码最后更新时间显示 - 支持显示格式:YYYY-MM-DD HH:MM:SS --- core/templates/core/base.html | 5 ++++ core/templatetags/__init__.py | 0 core/templatetags/git_info.py | 54 +++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 core/templatetags/__init__.py create mode 100644 core/templatetags/git_info.py diff --git a/core/templates/core/base.html b/core/templates/core/base.html index fddec62..16625a7 100644 --- a/core/templates/core/base.html +++ b/core/templates/core/base.html @@ -1,3 +1,4 @@ +{% load git_info %} @@ -535,6 +536,10 @@ 2026 家庭日报系统 - 专注于xiaji家庭生活的轻量级日报系统

+

+ + 代码最后更新:{% git_last_commit_time %} +

diff --git a/core/templatetags/__init__.py b/core/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/core/templatetags/git_info.py b/core/templatetags/git_info.py new file mode 100644 index 0000000..7efe002 --- /dev/null +++ b/core/templatetags/git_info.py @@ -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"