From 0753ab395561c89688a5c74e94db80573f47644f Mon Sep 17 00:00:00 2001 From: Cafw Date: Wed, 25 Feb 2026 22:01:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=BB=E7=95=8C=E9=9D=A2?= =?UTF-8?q?=E6=80=BB=E6=B5=8F=E8=A7=88=E6=95=B0=E4=B8=BA0=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gallery/templates/gallery/index.html | 4 +--- gallery/views.py | 4 +++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gallery/templates/gallery/index.html b/gallery/templates/gallery/index.html index 555e392..655c9ed 100755 --- a/gallery/templates/gallery/index.html +++ b/gallery/templates/gallery/index.html @@ -68,9 +68,7 @@
分类数量
- {% with total_views=artworks.aggregate.total_views %} -
{{ total_views|default:"0" }}
- {% endwith %} +
{{ total_views }}
总浏览次数
diff --git a/gallery/views.py b/gallery/views.py index 1ce0a8f..fe24a6e 100644 --- a/gallery/views.py +++ b/gallery/views.py @@ -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)