为什么我在我的Django个人博客系统在模板中渲染时间日期时出现了Could not parse the remainder: ' "Y-m-d H:i:s"' from 'now "Y-m-d H:i:s"'
<div class="mb-8">
<h2 class="text-2xl font-semibold text-blue-900 mb-4">
<i class="fa fa-bar-chart mr-2"></i> 统计
</h2>
<p class="text-gray-600 leading-relaxed">
截至 <span id="current-time">{{ now "Y-m-d H:i:s" }} <span id="current-seconds">:</span></span>,共发布
<span class="text-blue-900 font-bold">{{ total_articles }}</span> 篇文章,
累计 <span class="text-blue-900 font-bold">{{ total_words }}</span> 字。
</p>
</div>
// 1. 获取服务器时间(ISO格式,兼容不同时区)
const serverIsoTime = "{{ now "%Y-%m-%d %H:%M:%S" }}"; // 格式:2025-06-15T14:30:45
const serverUtcTimestamp = new Date(serverIsoTime).getTime(); // 转换为UTC时间戳
function updateRealTime() {
// 2. 获取客户端当前UTC时间戳
const clientUtcTimestamp = Date.now();
// 3. 计算时间差(毫秒)
const timeDiff = clientUtcTimestamp - serverUtcTimestamp;
// 4. 转换为时分秒(基于UTC偏移)
const totalSeconds = Math.floor(timeDiff / 1000);
const hours = Math.floor(totalSeconds / 3600) % 24; // 确保小时在0-23之间
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = totalSeconds % 60;
// 5. 补零处理
const pad = (n) => n.toString().padStart(2, '0');
const formattedTime = `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`;
// 6. 更新页面显示
document.getElementById('current-seconds').textContent = ` ${formattedTime}`;
}
// 初始化时立即更新
updateRealTime();
// 每秒更新一次
setInterval(updateRealTime, 1000);
def about_view(request):
# 统计文章总数
total_articles = Article.objects.count()
# 统计总字数(通过聚合函数计算 content 字段总长度)
from django.db.models import Sum, CharField, Func
class Length(Func):
function = 'LENGTH'
output_field = CharField()
total_words = Article.objects.aggregate(total=Sum(Length('content')))['total'] or 0
# # 今日文章数(按服务器时区)
# today = timezone.now().date()
# today_articles = Article.objects.filter(create_time__date=today).count()
return render(request, 'about.html', {
'total_articles': total_articles,
'total_words': total_words,
'latest_article_time': Article.objects.latest('create_time').create_time if total_articles else None,
})
目前截图
