在the fourth part of the Django tutorial中,它使用django.views.generic.ListView
,但在Class-based views API reference中,ListView
位于django.views.generic.list.ListView
。 django.views.generic.list.ListView
和django.views.generic.ListView
之间有什么区别?
答案 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
导入任何或所有类,而无需引用单个模块。