Рейтинг книг
This commit is contained in:
@@ -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 %}
|
||||
|
||||
9
bookify/books/templates/books/books_rating.html
Normal file
9
bookify/books/templates/books/books_rating.html
Normal 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 %}
|
||||
@@ -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"),
|
||||
]
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user