Django Class Based Views - 1

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.shortcuts import render
from django.views.generic import View, TemplateView, ListView


#def product_list(request):
#    return render(request=request, template_name="products.html")

class ProductView(View):
    http_method_names = ["get", "post"]

    def dispatch(self, request, *args, **kwargs):
        print(request.method)
        return super().dispatch(request=request, *args, **kwargs)

    def get(self, request):
        print("GET METHODU")
        return render(request=request, template_name="products.html")

    def post(self, request):
        return render(request, "products.html")

class AboutView(TemplateView):
    template_name = "about.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        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">
    <title>Document</title>
</head>
<body>
Hakkımda

{{ dynamic_data }}
</body>
</html>

Comments

    There are no comments yet.

Comment