Tony
Tony

Reputation: 21

Function Based View to Class Based View

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

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

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

Related Questions