Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the advantage of Class-Based views?
    primarykey
    data
    text
    <p>I read today that Django 1.3 alpha is shipping, and the most touted new feature is the introduction of <a href="http://docs.djangoproject.com/en/dev/releases/1.3-alpha-1/#class-based-views">class-based views</a>.<br> I've read the <a href="http://docs.djangoproject.com/en/dev/topics/class-based-views/">relevant documentation</a>, but I find difficult to see the <em>big advantage™</em> that I could get by using them, so I'm asking here for some help in understanding them.<br> Let's take an <a href="http://docs.djangoproject.com/en/dev/topics/class-based-views/#viewing-subsets-of-objects">advanced example</a> from the documentation.</p> <h2>urls.py</h2> <pre><code>from books.views import PublisherBookListView urlpatterns = patterns('', (r'^books/(\w+)/$', PublisherBookListView.as_view()), ) </code></pre> <h2>views.py</h2> <pre><code>from django.shortcuts import get_object_or_404 from django.views.generic import ListView from books.models import Book, Publisher class PublisherBookListView(ListView): context_object_name = "book_list" template_name = "books/books_by_publisher.html", def get_queryset(self): self.publisher = get_object_or_404(Publisher, name__iexact=self.args[0]) return Book.objects.filter(publisher=self.publisher) def get_context_data(self, **kwargs): # Call the base implementation first to get a context context = super(PublisherBookListView, self).get_context_data(**kwargs) # Add in the publisher context['publisher'] = self.publisher return context </code></pre> <p>And now let's compare it to a “plain-old-views” solution, made by myself in 5 minutes for this question (I apologize for any error you may find in it).</p> <h2>urls.py</h2> <pre><code>urlpatterns = patterns('books.views', url(r'^books/(\w+)/$', 'publisher_books_list', name="publisher_books_list"), ) </code></pre> <h2>views.py</h2> <pre><code>from django.shortcuts import get_object_or_404 from books.models import Book, Publisher def publisher_books_list(request, publisher_name): publisher = get_object_or_404(Publisher, name__iexact=publisher_name) book_list = Book.objects.filter(publisher=publisher) return render_to_response('books/books_by_publisher.html', { "book_list": book_list, "publisher": publisher, }, context_instance=RequestContext(request)) </code></pre> <p>The second version to me looks:</p> <ul> <li>Equivalent in functionality</li> <li>A lot more readable (<code>self.args[0]</code>? awful!)</li> <li>Shorter</li> <li>Not less DRY-compliant</li> </ul> <p>Is there something big I'm missing? Why should I use them? Are those on the documentation? If so then what would be the ideal use case? Are <em>mixins</em> that useful?</p> <p>Thanks in advance to anybody who contributes!</p> <p><strong>P.S.</strong> for those who might wonder, I was never enthralled by generic views as well: as soon as I needed some advanced functionality, they became no shorter than regular views.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload