Рейтинг книг

This commit is contained in:
2025-01-16 18:56:06 +03:00
parent c41188e17b
commit b1e5486d27
4 changed files with 24 additions and 1 deletions

View File

@@ -11,6 +11,7 @@
<header>
<h1><a href="{% url 'books:book_list' %}">Bookify</a></h1>
<nav>
<a href="{% url 'books:books_rating' %}">Рейтинг книг</a>
<a href="{% url 'books:book_list' %}">Список книг</a>
<a href="{% url 'books:genre_list' %}">Список жанров</a>
{% if user.is_authenticated %}

View File

@@ -0,0 +1,9 @@
{% extends 'books/base.html' %}
{% block content %}
<h2>Общий рейтинг книг</h2>
<div class="book-list">
{% for book in books %}
{% include 'books/_book_item.html' %}
{% endfor %}
</div>
{% endblock %}

View File

@@ -25,4 +25,5 @@ urlpatterns = [
path("succesful-logout/", views.logout, name="logout"),
path("book/<int:pk>/edit/", views.edit_book, name="edit_book"),
path("review/<int:pk>/edit/", views.edit_review, name="edit_review"),
path("rating/", views.books_rating, name="books_rating"),
]

View File

@@ -2,6 +2,7 @@ from django.contrib.auth import login
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import AuthenticationForm
from django.core.exceptions import PermissionDenied
from django.db.models import Avg
from django.shortcuts import get_object_or_404, redirect, render
from .forms import BookForm, CustomUserCreationForm, GenreForm, ReviewForm
@@ -25,6 +26,17 @@ def logout(requst):
return render(requst, "accounts/logout.html")
def books_rating(request):
"""
Страница со всеми книгами, отсортированными по убыванию среднего рейтинга.
"""
# Переименуем аннотацию в avg_rating
books = Book.objects.annotate(avg_rating=Avg("reviews__rating")).order_by(
"-avg_rating"
)
return render(request, "books/books_rating.html", {"books": books})
def book_list(request):
"""Главная страница со списком всех книг."""
books = Book.objects.all()