Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - use generic views or not?

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?

like image 995
Ska Avatar asked Jun 26 '11 15:06

Ska


People also ask

Why do we need generic views in Django and where is the best use case?

The generic class-based-views was introduced to address the common use cases in a Web application, such as creating new objects, form handling, list views, pagination, archive views and so on. They come in the Django core, and you can implement them from the module django.

Should I use Django class-based views?

Class based views are excellent if you want to implement a fully functional CRUD operations in your Django application, and the same will take little time & effort to implement using function based views.

Which view is better in Django?

1. Function-Based Views. Function-based views are good for beginners. It is very easy to understand in comparison to class-based views.

What is the difference between a list view and detail view in Django?

Generally, Detail is for 1 item, List is for many.


3 Answers

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.

like image 53
lprsd Avatar answered Sep 21 '22 11:09

lprsd


Whether to use generic views or not is your prerogative. It won't cause you any trouble, although you might find yourself coding repetitious view logic. You might consider using wrapped/subclassed generic views in your views.py (frequently you'll want to customize them anyways), which would keep the boilerplate out of your urls.py and all the views in the same place.

like image 41
zeekay Avatar answered Sep 17 '22 11:09

zeekay


In django 1.2 I use generic views but inside a "normal" view , not in urls, like :

#views.py
import generic_views

def my_generic_list(request):
    qs = Something.objects.filter(some arguments)
    return generic.object_list(queryset = qs, ... other stuff, usually extra_context)

this way (imo) the view is very simple yet can become a "real one" in case of changes, while the urls.py remain clean

like image 34
simone cittadini Avatar answered Sep 17 '22 11:09

simone cittadini