修复主界面总浏览数为0问题

This commit is contained in:
2026-02-25 22:01:58 +08:00
parent 02cc29fcfd
commit 0753ab3955
2 changed files with 4 additions and 4 deletions

View File

@@ -68,9 +68,7 @@
<div class="text-gray-400">分类数量</div>
</div>
<div class="text-center">
{% with total_views=artworks.aggregate.total_views %}
<div class="text-3xl font-bold text-blue-400 mb-2">{{ total_views|default:"0" }}</div>
{% endwith %}
<div class="text-3xl font-bold text-blue-400 mb-2">{{ total_views }}</div>
<div class="text-gray-400">总浏览次数</div>
</div>
<div class="text-center">

View File

@@ -1,6 +1,6 @@
from django.shortcuts import render, get_object_or_404, redirect
from django.views.generic import ListView, DetailView
from django.db.models import Q
from django.db.models import Q, Sum
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.contrib import messages
@@ -12,8 +12,10 @@ from .forms import CommentForm
def index(request):
"""首页视图 - 展示所有作品"""
artworks = Artwork.objects.all().order_by('order', '-created_at')
total_views = artworks.aggregate(total=Sum('view_count'))['total'] or 0
context = {
'artworks': artworks,
'total_views': total_views,
'page_title': 'YITAO-REN GALLERY',
}
return render(request, 'gallery/index.html', context)