2

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)

1 Answer 1

1

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'
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!!! I'm pretty new to Django this helped out alot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.