Django Class Based Views - 2

Model:

from django.db import models

class Product(models.Model):
    name = models.CharField(verbose_name="Ürün", max_length=255)
    count = models.PositiveIntegerField()


    def __str__(self) -> str:
        return self.name

View:

from django.db.models import QuerySet
from django.shortcuts import render
from django.views.generic import View, TemplateView, ListView

from product.models import Product


class ProductListView(ListView):
    template_name = "products.html"
    context_object_name = "products"
    paginate_by = 2
    page_kwarg = "sayfa"

    def get_queryset(self) -> QuerySet:
        count = self.request.GET.get("count")
        if count:
            count = int(count)
            return Product.objects.filter(count__gt=count)
        return Product.objects.all()

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super().get_context_data()
        context["key"] = "hello world"
        return context

 

Html:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css"
          integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
    <title>Document</title>
</head>
<body>

<div class="container mt-5">
    <h3 class="text-center">Ürün Listele</h3>
{% for product in products %}
{{ product.name }} --- {{ product.count }}
    <br>
{% endfor %}
</div>
{{ key }}
</body>
</html>

Comments

    There are no comments yet.

Comment