def post(self, request): if request.user.is_authenticated: bf = BookForm(data=request.POST, files=request.FILES) book = bf.save(commit=True) book.authors.add(request.user) book.save() return redirect("the-main-page")
def post(self, request, slug): if request.user.is_authenticated: book = Book.objects.get(slug=slug) if request.user in book.authors.all(): bf = BookForm(instance=book, data=request.POST) if bf.is_valid(): bf.save(commit=True) return redirect('the-main-page')
def post(self, request): if request.user.is_authenticated: bf = BookForm(request.POST, request.FILES) if bf.is_valid(): book = bf.save(commit=True) book.authors.add(request.user) book.save() return redirect("the-main-page") messages.error(request, "книга с таким slug уже существует!!! измените slug") return redirect("the-main-page")
def get(self, request): context = {} books = Book.objects.prefetch_related("authors", "genre") gen = Genre.objects.all() # Проверка на владельца книги для удаления/редактирования if request.user.is_authenticated: is_owner = Exists( User.objects.filter(books=OuterRef("pk"), id=request.user.id)) books = books.annotate(is_owner=is_owner) # Добавление постраничного вывода page = request.GET.get('page', 1) books = Paginator(books, 7) try: books = books.get_page(page) except PageNotAnInteger: books = books.page(1) except EmptyPage: books = books.page(books.num_pages) context['books'] = books context['range'] = range(1, 6) context['form'] = BookForm() context['gen'] = gen context['page'] = page return render(request, "index.html", context)
def get(self, request, slug): if request.user.is_authenticated: book = Book.objects.get(slug=slug) if request.user in book.authors.all(): form = BookForm(instance=book) return render(request, "update_book.html", {"form": form, "slug": book.slug}) return redirect('the-main-page')
def get(self, request): context = {} comment_query = Comment.objects.annotate( count_like=Count("users_like")).select_related("author") comments = Prefetch("comments", comment_query) context['books'] = Book.objects.prefetch_related("authors", comments) context['range'] = range(1, 6) context['form'] = BookForm() return render(request, "index.html", context)
def get(self, request): context = {} books = Book.objects.prefetch_related("authors") if request.user.is_authenticated: is_owner = Exists(User.objects.filter(books=OuterRef('pk'), id=request.user.id)) books = books.annotate(is_owner=is_owner) context['books'] = books.order_by("rate", "date") #sorted context['range'] = range(1, 6) context['form'] = BookForm() return render(request, "index.html", context)
def get(self, request): context = {} books = Book.objects.prefetch_related("authors") if request.user.is_authenticated: is_owner = Exists( User.objects.filter(books=OuterRef("pk"), id=request.user.id)) books = books.annotate(is_owner=is_owner) paginator = Paginator(books.order_by("-rate", "date"), 5) # CREATE PAGINATOR context['books'] = paginator.get_page(request.GET.get( "page", 1)) # CREATE PAGINATOR context['range'] = range(1, 6) context['form'] = BookForm() return render(request, "index.html", context)
def get(self, request): context = {} books = Book.objects.prefetch_related('authors', 'genre') gen = Genre.objects.all() if request.user.is_authenticated: is_owner = Exists( User.objects.filter(books=OuterRef('pk'), id=request.user.id)) books = books.annotate(is_owner=is_owner) paginator = Paginator(books.order_by('data'), 5) context['books'] = paginator.get_page(request.GET.get('page', 1)) context['range'] = range(1, 6) context['form'] = BookForm() context['gen'] = gen return render(request, 'index.html', context)
def get(self, request, genre): books = Book.objects.filter(genre__text=genre) context = {} books = books.prefetch_related('authors', 'genre') gen = Genre.objects.all() if request.user.is_authenticated: is_owner = Exists( User.objects.filter(books=OuterRef('pk'), id=request.user.id)) books = books.annotate(is_owner=is_owner) context['books'] = books.order_by('data') context['range'] = range(1, 6) context['form'] = BookForm() context['gen'] = gen return render(request, 'page_books_genre.html', context)
def get(self, request, genre): context = {} genres_all = Genre.objects.all() genre = genres_all.get(genre=genre) books = Book.objects.prefetch_related("authors", "genres") if request.user.is_authenticated: is_owner = Exists( User.objects.filter(books=OuterRef('pk'), id=request.user.id)) is_liked = Exists( User.objects.filter(liked_books=OuterRef('pk'), id=request.user.id)) books = books.annotate(is_owner=is_owner, is_liked=is_liked) context['books'] = books.filter(genres=genre).order_by("-rate", "date") context['range'] = range(1, 6) context['form'] = BookForm() context['login_form'] = AuthenticationForm() context['genres_all'] = genres_all context['genre'] = genre return render(request, "books_genre_filter.html", context)
def get(self, request): context = {} genres_all = Genre.objects.all() books = Book.objects.prefetch_related("authors", "genres") if request.user.is_authenticated: is_owner = Exists( User.objects.filter(books=OuterRef('pk'), id=request.user.id)) is_liked = Exists( User.objects.filter(liked_books=OuterRef('pk'), id=request.user.id)) books = books.annotate(is_owner=is_owner, is_liked=is_liked) books = books.order_by("-rate", "date") context['range'] = range(1, 6) context['form'] = BookForm() context['login_form'] = AuthenticationForm() context['genres_all'] = genres_all paginator = Paginator(books, 3) page_number = request.GET.get('page', 1) # number of page or 1 page_obj = paginator.get_page(page_number) context['page_obj'] = page_obj return render(request, "index.html", context)
def get(self, request, genre): books = Book.objects.filter(genre__name_genre=genre) context = {} books = books.prefetch_related('authors', 'genre') gen = Genre.objects.all() if request.user.is_authenticated: is_owner = Exists( User.objects.filter(books=OuterRef('pk'), id=request.user.id)) books = books.annotate(is_owner=is_owner) # Добавление постраничного отображения page = request.GET.get('page', 1) books = Paginator(books, 7) try: books = books.get_page(page) except PageNotAnInteger: books = books.page(1) except EmptyPage: books = books.page(books.num_pages) context['books'] = books context['range'] = range(1, 6) context['form'] = BookForm() context['gen'] = gen context['page'] = page return render(request, 'page_books_genre.html', context)