Reputation: 21
How would I convert my Function Based View into Class Based View.
urls.py
urlpatterns = [
path('', views.SlideShowView, name="slideshow"),
]
models.py
class Carousel(models.Model):
title = models.CharField(max_length=255, blank=True)
description = models.TextField(max_length=255, blank=True)
image = models.ImageField(upload_to="showcase/%y/%m/%d/", blank=True)
def __str__(self):
return self.title
views.py
def SlideShowView(request):
carousel = Carousel.objects.all()
context = {
'carousel' : carousel,
}
return render(request, "showcase.html", context)
Upvotes: 2
Views: 52
Reputation: 476527
This is exactly covered by a ListView
[Django-doc]:
from django.views.generic.list import ListView
class SlideShowView(ListView):
model = Carousel
context_object_name = 'carousel'
template_name = 'showcase.html'
Upvotes: 1