Create class based view in django 3

create-class-based-view-in-django

Hello World, Welcome to projects plaza.com. Today we will learn how to create a class-based view in Django 3. In this tutorial, we will render a simple template via class. Basically, there are two methods to load the view template in Django. One is a function-based view(FBV) and another is a class-based view(CBV). Class-based views were introduced in Django 1.7. The reason behind this was the proper structure, inheritance and the developer can customize the class-based view more than the function-based view.

  • Create cbv.html in the templates¬†folder in your app and add the following code.
  • <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>CBV View</title>
    </head>
    <body>
        <h3>Hello World, This is class based view</h3>
    </body>
    </html>

     

  • Open app/urls.py and add the following code in it
  • urlpatterns=[
        ...
        path('cbv',TemplateView.as_view(template_name="cbv.html"))
    ]

     

Leave a Reply