Search code examples
pythondjango-views

Why can't I import generic from my Django views.py file?


So when I try to import from Django's generic file library it gives me an error message saying:

"ImportError: cannot import name 'generic' from 'django.views.generic' (C:\Users\USER\Desktop\djang0\venv\Lib\site-packages\django\views\generic_init_.py)"

The below code is in my views.py

from django.views.generic import generic

class LeadListView(generic.ListView):
    template_name = "leads/lead_list.html"
    queryset = Lead.objects.all()
    content_object_name = "leads"

Before I was all individual views like shown below, and my code run with no errors. But that gets tiring and is repetitive. Any idea why Django cannot import generic? Only information I found was that this can be solved by adding top_level_module=True in import statement, with no explanations. I'm not sure if this should be added in seetings.py file or my views.py or how to even add it. Note: I'm new to Django this is my first project.

This worked. Also from views.py

from django.views.generic import ListView, CreateView, DetailView

class LeadListView(ListView):
    template_name = "leads/lead_list.html"
    queryset = Lead.objects.all()
    content_object_name = "leads"

Solution

  • You can, but generic has no submodule named generic. You import generic with:

    from django.views import generic