I was going through quick poll tutorial on the Django site, and the last topic is introduction of generic views. A convenient way to bypass the need of creation of custom views for every URL pattern.
This is the main idea as far as I understand:
1) Request -> URL patterns -> View -> Template
or
2) Request -> URL patterns (Generic View) [-> optional Template]
2 seems to require less code, it’s only two steps as opposed to four, but on the downside you’re sticking more stuff into URL patterns, there’s more automagic going on, and your views are now defined in two places.
I really like the idea of having URL Patterns just that – patterns, and not adding in additional boilerplate. I also like the idea of having all Views explicitly defined, even the simple ones, so that later I know where to find them all without going back and forth through files. Plus we all know that any automagic is harder to customise than something you build from scratch (at least from Django scratch).
Am I missing something? Am I doing a big mistake that will haunt me later of I don’t use generic views at all?
The intention of Generic Views is to reduce boilerplate code when you repeatedly use similar code in several views. You should really use it just for that. Basically, just because django allows something you are doing generically you shouldn’t do it, particularly not when your code becomes not to your like.
If you are using django-1.3’s Class Based Views, instead of passing many variables to the function in
urls.py, you can override respective methods of interest, which provides the best of both worlds. – Less code, and more control.