7
7
7a-6662020-04-16 12:40:42
Django
7a-666, 2020-04-16 12:40:42

Paginator for detail view?

I have ListView product categories when I go to a specific DetailView catalog
In the catalog I load the products related to it through product_set.all I need to do pagination for the products
The catalog and the products of 2 different applications but they are connected. The catalog has views about the list of categories and one category, and in the products application there is one view of the product card, that is, DetailView Category
code

app_name = 'catalog'

urlpatterns = [
    path('', CategoriesList.as_view(), name='categories_list'),
    path('<str:slug>/', CategoryDetail.as_view(), name='category_detail'),
]

class CategoriesList(ListView):
    model = Category
    context_object_name = 'categories'
    template_name = 'catalog/categories_list.html'


class CategoryDetail(DetailView):
    model = Category
    template_name = 'catalog/category_detail.html'


Product code
app_name = 'product'

urlpatterns = [

    # Перенаправление в каталог
    path('', RedirectView.as_view(url='/catalog/')),

    path('<str:slug>/', ProductDetail.as_view(), name='detail'),
]

class ProductDetail(DetailView):
    """Карточка товара"""
    
    model = Product
    template_name = 'products/products_detail.html'
    
    def get_context_data(self, **kwargs):
        """Добавляю представление о форме ввода комментария"""

        context = super().get_context_data(**kwargs)
        context['comment_form'] = CommentForm()
        return context

And now the questions
1) Is it better to override the DetailView of the catalog in a ListView and do pagination?
1.1) Then, according to the logic, in which application should this view be located, because it is both a list of products and a category

2) Make pagination in DetailView, then tell me how to do it preferably in detail. I looked at the answers and the code, but I can't figure out how it works here are the links
- https://stackoverflow.com/questions/25569551/pagin...
- https://gist.github.com/nspo/5ab1ecde7e918a5fa2662.. .- https://www.reddit.com/r/django/comments/6tdavj/he
...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2020-04-16
@7a-666

1) Yes
1.1) It depends primarily on where your models are imported from - this is the most common reason for cyclic imports.
2) You can use the paginator directly, in much the same way as it was done in the ListView, only set it on category.product_set.all(). Of course everything is done by hand.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question