1

So below I have just created by first Class based view after watching a Django Con EU talk video.

It works and understand what it does. I don't understand the difference between a Class based view or a generic class base view - which I have just build?

class GroupListView(ListView):
    """
    List all Groups.
    """
    context_object_name = 'groups'
    template_name = 'contacts/home.html'

    def get_context_data(self, **kwargs):
        """
        Get the context for this view.
        """
        # Call the base implementation first to get a context.
        context = super(GroupListView, self).get_context_data(**kwargs)
        # Add more contexts.
        context['tasks'] = Upload.objects.filter(uploaded_by=self.request.user).order_by('-date_uploaded')[:5]
        context['unsorted'] = Contact.objects.unsorted_contacts(user=self.request.user).count()

        return context

    def get_queryset(self):
        """
        Get the list of items for this view. This must be an iterable, and may
        be a queryset (in which qs-specific behavior will be enabled).
        """
        queryset = Group.objects.for_user(self.request.user)
        return queryset 
GrantU
  • 6,325
  • 16
  • 59
  • 89

3 Answers3

4

Django comes with a set of views that are trying to solve some common problems, eg:

  • Editing an object
  • Displaying a single object
  • Displaying a list of objects

Because these views can be used for different models, they are called generic. "Generic" in this case hasn't anything to do with the way they are implemented - in older Django version function views were the default (the views were functions), now they have became classes.

Your example view is actually a class-based view that inherits from a class-based generic view.

Bernhard Vallant
  • 49,468
  • 20
  • 120
  • 148
  • I see, this is where my confusion is coming from. So if I was just using a generic view I don't see how I would add any more functionally like in my get_context_data super etc. So what your saying is I could just just a class view without the inheritance of a generic class? Any reason why not to do this? – GrantU Jun 19 '13 at 13:15
  • 1
    If you need any functionality that is already provided by a generic view you'll most probably try to use this and maybe modify it. If you want to create your "own" view from scratch inherit from `django.views.generic.base.View` (guess it is a bit confusing that it lives in `views.generic`). [This](http://ccbv.co.uk/) is a good starting point if you would like to browse through the views... – Bernhard Vallant Jun 19 '13 at 13:19
  • 1
    https://docs.djangoproject.com/en/dev/topics/class-based-views/intro/#using-class-based-views – Bernhard Vallant Jun 19 '13 at 13:21
3

A Class based view is any Django view provided as a Python Class (as opposed to views as functions).

A Generic base View in Django is the base for any of the built-in views which you can use for your convenience. Examples:

  • ListView
  • CreateView
  • DetailView

Those are based on django.views.generic.base.View.

gertvdijk
  • 24,056
  • 6
  • 41
  • 67
1

ListView is a generic class-based view (which means, it comes with Django, implements very common features, and is free to be inherited from for more precise handling as you did). On the other hand, GroupListView is not generic - as it is not part of Django.

Vincent
  • 430
  • 3
  • 13