Django - use general views or not?

I went through a quick survey tutorial on the Django website, and the last topic was introducing general concepts. A convenient way to get around the need to create custom views for each URL pattern.

This is the main idea, as I understand it:

1) Query β†’ URL Patterns β†’ View β†’ Pattern

or

2) Request β†’ URL patterns (general view) [-> optional pattern]

2, apparently, requires less code, it's just two steps, not four, but on the other hand you paste more into URL patterns, there is more automatic continuation, and now your views are defined in two places.

I really like the idea of ​​having URL patterns, that is what the patterns are, rather than adding an extra pattern. I also like the idea of ​​uniquely identifying all views, even simple ones, so later I know where to find them without going through the files. Plus, we all know that any machine is harder to set up than something that you build from scratch (at least from scratch Django).

Am I missing something? I am making a big mistake that will haunt me later, I don’t use general views at all?

+7
source share
3 answers

The goal of Generic Views is to reduce template code when you reuse similar code in multiple views. You really should use it just for that. Basically, just because django permits what you do as a whole, you should not do this, especially if your code does not fit yours.

If you use django-1.3 Class Based Views, instead of passing many variables to a function in urls.py , you can override the corresponding methods of interest that provide the best of both worlds. - Less code and more control.

+10
source

Whether to use general concepts or not is your prerogative. This will not cause you any problems, although you may find that the coding of repetitive presentation logic. You can consider using generic views with a wrapper / subclass in your view.py (often you want to customize them anyway), which will cause the template template to not be deleted from your urls.py and all the views in the same location.

+3
source

In django 1.2, I use generic views, but inside a "normal" view, and not in URLs, for example:

 #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, but can become "real" in case of changes, and urls.py will remain clean.

+2
source

All Articles