Django - 是否使用通用视图?

时间:2021-11-27 19:16:15

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.

我正在浏览Django网站上的快速民意调查教程,最后一个主题是通用视图的介绍。一种方便的方法来绕过为每个URL模式创建自定义视图的需要。

This is the main idea as far as I understand:

据我了解,这是主要想法:

1) Request -> URL patterns -> View -> Template

1)请求 - > URL模式 - >视图 - >模板

or

要么

2) Request -> URL patterns (Generic View) [-> optional Template]

2)请求 - > URL模式(通用视图)[ - >可选模板]

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.

2似乎需要更少的代码,它只有两个步骤,而不是四个,但是在缺点上你会更多的东西进入URL模式,更多的自动化正在进行,你的视图现在定义在两个地方。

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).

我真的很喜欢这样的URL模式 - 模式,而不是添加额外的样板。我也喜欢明确定义所有视图的想法,即使是简单的视图,以便后来我知道在不经过文件的情况下在哪里找到它们。另外我们都知道,任何自动化都比你从头开始构建的东西更难定制(至少从Django划痕)。

Am I missing something? Am I doing a big mistake that will haunt me later of I don't use generic views at all?

我错过了什么吗?我是否犯了一个很大的错误,以后我不会使用通用视图?

3 个解决方案

#1


10  

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.

Generic Views的目的是在多个视图中重复使用类似代码时减少样板代码。你应该真正使用它。基本上,只是因为django允许你正在做的事情,你不应该这样做,特别是当你的代码变得不那样时。

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.

如果您使用django-1.3的基于类的视图,而不是将许多变量传递给urls.py中的函数,您可以覆盖相应的感兴趣的方法,这提供了两全其美的优势。 - 更少的代码和更多的控制。

#2


3  

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.

是否使用通用视图是您的特权。虽然您可能会发现自己编写重复的视图逻辑,但它不会给您带来任何麻烦。您可以考虑在views.py中使用包装/子类通用视图(通常您仍然希望自定义它们),这样可以将样板文件保留在urls.py之外,并将所有视图保留在同一位置。

#3


2  

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

在django 1.2中,我使用通用视图但在“普通”视图中,而不是在URL中,例如:

#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

这种方式(imo)视图非常简单,但在变化的情况下可以成为“真实的”,而urls.py保持清洁

#1


10  

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.

Generic Views的目的是在多个视图中重复使用类似代码时减少样板代码。你应该真正使用它。基本上,只是因为django允许你正在做的事情,你不应该这样做,特别是当你的代码变得不那样时。

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.

如果您使用django-1.3的基于类的视图,而不是将许多变量传递给urls.py中的函数,您可以覆盖相应的感兴趣的方法,这提供了两全其美的优势。 - 更少的代码和更多的控制。

#2


3  

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.

是否使用通用视图是您的特权。虽然您可能会发现自己编写重复的视图逻辑,但它不会给您带来任何麻烦。您可以考虑在views.py中使用包装/子类通用视图(通常您仍然希望自定义它们),这样可以将样板文件保留在urls.py之外,并将所有视图保留在同一位置。

#3


2  

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

在django 1.2中,我使用通用视图但在“普通”视图中,而不是在URL中,例如:

#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

这种方式(imo)视图非常简单,但在变化的情况下可以成为“真实的”,而urls.py保持清洁