Рейтинг книг
This commit is contained in:
@@ -11,8 +11,9 @@
|
|||||||
<header>
|
<header>
|
||||||
<h1><a href="{% url 'books:book_list' %}">Bookify</a></h1>
|
<h1><a href="{% url 'books:book_list' %}">Bookify</a></h1>
|
||||||
<nav>
|
<nav>
|
||||||
|
<a href="{% url 'books:books_rating' %}">Рейтинг книг</a>
|
||||||
<a href="{% url 'books:book_list' %}">Список книг</a>
|
<a href="{% url 'books:book_list' %}">Список книг</a>
|
||||||
<a href="{% url 'books:genre_list' %}">Список жанров</a>
|
<a href="{% url 'books:genre_list' %}">Список жанров</a>
|
||||||
{% if user.is_authenticated %}
|
{% if user.is_authenticated %}
|
||||||
<a href="{% url 'books:add_book' %}">Добавить книгу</a>
|
<a href="{% url 'books:add_book' %}">Добавить книгу</a>
|
||||||
<a href="{% url 'logout' %}">Выйти</a>
|
<a href="{% url 'logout' %}">Выйти</a>
|
||||||
|
|||||||
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("succesful-logout/", views.logout, name="logout"),
|
||||||
path("book/<int:pk>/edit/", views.edit_book, name="edit_book"),
|
path("book/<int:pk>/edit/", views.edit_book, name="edit_book"),
|
||||||
path("review/<int:pk>/edit/", views.edit_review, name="edit_review"),
|
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.decorators import login_required
|
||||||
from django.contrib.auth.forms import AuthenticationForm
|
from django.contrib.auth.forms import AuthenticationForm
|
||||||
from django.core.exceptions import PermissionDenied
|
from django.core.exceptions import PermissionDenied
|
||||||
|
from django.db.models import Avg
|
||||||
from django.shortcuts import get_object_or_404, redirect, render
|
from django.shortcuts import get_object_or_404, redirect, render
|
||||||
|
|
||||||
from .forms import BookForm, CustomUserCreationForm, GenreForm, ReviewForm
|
from .forms import BookForm, CustomUserCreationForm, GenreForm, ReviewForm
|
||||||
@@ -25,6 +26,17 @@ def logout(requst):
|
|||||||
return render(requst, "accounts/logout.html")
|
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):
|
def book_list(request):
|
||||||
"""Главная страница со списком всех книг."""
|
"""Главная страница со списком всех книг."""
|
||||||
books = Book.objects.all()
|
books = Book.objects.all()
|
||||||
|
|||||||
Reference in New Issue
Block a user