Tuesday, January 3, 2023

How to show all records in a model and individual record using class based views

 Instead of following function based view logic in link, we can use predefined logic from class based views.


views.py

from django.views.generic import ListView,DetailView

class NotesListView(ListView):
  model = Notes
  context_object_name = "notes"
  template_name = "notes/notes_list.html" ##optional to pass template as uses default template

class NotesDetailView(DetailView):
  model = Notes
  context_object_name = "note"

And the url patterns file changes as below.


urlpatterns = [
path('notes',views.NotesListView.as_view()),
 path('notes/<int:pk>',NotesDetailView.as_view()),
]

No comments:

Post a Comment