Search code examples
pythondjangopython-importdjango-class-based-views

Confusion when importing Django' generic class based views


While exploring the reference documentation and some tutorials I have seen the import for Django's generic class based views used in multiple ways:

from django.views.generic import TemplateView, ListView

Or:

from django.views.generic.base import TemplateView
from django.views.generic.list import ListView

I have tried experimenting with both tactis and they both seem to work, yet the first option seems less correct.

Do all the generic CBVs also exist within the generic module a well as the submodules? Can Python import classes from submodules by only importing a higher module? What is actually happening here?

Any help would be greatly appreciated!


Solution

  • After some digging it seems that within django.views.generic.__init__.py all the other generic CBVs are imported allowing intentional access to them within django.views.generic. This seems to be the standardised way of importing the generic CBVs.