Django中的django.views.generic.list.ListView和django.views.generic.ListView有什么区别?

时间:2017-05-11 08:29:42

标签: python django

the fourth part of the Django tutorial中,它使用django.views.generic.ListView,但在Class-based views API reference中,ListView位于django.views.generic.list.ListViewdjango.views.generic.list.ListViewdjango.views.generic.ListView之间有什么区别?

2 个答案:

答案 0 :(得分:1)

两者都正好引用相同的类。您可以通过

进行检查
import inspect
from django.views.generic import ListView
print(inspect.getfile(ListView))
from django.views.generic.list import ListView
print(inspect.getfile(ListView)) 

答案 1 :(得分:1)

ListView课程实际上位于django/views/generic/list.py。但这是django/views/generic/__init__.py的源代码:

from django.views.generic.base import RedirectView, TemplateView, View
from django.views.generic.dates import (
    ArchiveIndexView, DateDetailView, DayArchiveView, MonthArchiveView,
    TodayArchiveView, WeekArchiveView, YearArchiveView,
)
from django.views.generic.detail import DetailView
from django.views.generic.edit import (
    CreateView, DeleteView, FormView, UpdateView,
)
from django.views.generic.list import ListView

__all__ = [
    'View', 'TemplateView', 'RedirectView', 'ArchiveIndexView',
    'YearArchiveView', 'MonthArchiveView', 'WeekArchiveView', 'DayArchiveView',
    'TodayArchiveView', 'DateDetailView', 'DetailView', 'FormView',
    'CreateView', 'UpdateView', 'DeleteView', 'ListView', 'GenericViewError',
]


class GenericViewError(Exception):
    """A problem in a generic view."""
    pass

如您所见,它从各自的模块导入所有通用视图。这只是一种便利,它允许您从django.views.generic导入任何或所有类,而无需引用单个模块。