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)