I don’t like Django class-based generic views.
Some caveats: I’m not the sharpest knife in the drawer. I’m not FSM’s gift to web development. I have a lot of experience with the function-based generic views, and little experience with the class-based ones. (Because they’re new. Duh.)
From yesterday’s experience, the new generic views use a very powerful but excessively complicated abstraction. I can only symptomatically describe the problem and I don’t have a good answer, but this is my blog so I’m going to bitch about it. If you don’t agree then move along, these aren’t the droids you’re looking for.
My task was simple: Code a, “display detail about an object,” page. The object is a db table row. That’s all.
After two hours, I wanted to stab my eyes with live yellow jackets.
I wanted this page accessible to only logged-in users, so I needed to override as_view() and use @login_required(). I also needed one of our decorators to verify that the user owned the db row. And, of course, I had to pass the db row object (from a Foobar.objects.get(yadda yadda yadda) call) to the as_view(). And use url-reversing in the urlconf, which turns out to have multiple bugs with parameterized regex’s, but I can’t blame that on class-based views.
Subclass the class, override the methods, specify the template. Easy! Well, no. I nearly got it working after three hours (some of that time was spent hunting the url-reverse bug). I then gave up and wrote a dead-simple view to display the template.
The kicker: The dead-simple view took fewer lines of code than the class-based generic view.
Something’s wrong here. This shouldn’t result in more lines of code. I love DRY as much as the next Pythonista, but I shouldn’t need an organic chemistry degree to figure out how to display a simple template.
I want to use generic views to quickly and simply display simple pages. I can’t do that anymore.
I don’t need über configuration flexibility in my generic views; I need simplicity. A couple of other folks have felt a similar unease. Agos asked about this on Stack Overflow. Luke Plant wrote about it on his blog.