I have following error while i try to reach PizzaDetailView on template: AttributeError at /pizza/6/ type object ‘PizzaMenu’ has no attribute ‘_default_manager’.
Where is the problem?
models.py
class PizzaMenu(models.Model): name = models.CharField(max_length=30) description = models.TextField() ingredients = models.CharField(max_length=100) price = models.DecimalField(max_digits=4, decimal_places=2) def __str__(self): return self.name class Meta: ordering = ["price"]
views.py
from django.views.generic import TemplateView, ListView, DetailView from .models import PizzaMenu class IndexView(TemplateView): template_name = "index.html" class PizzaMenu(ListView): model = PizzaMenu template_name = "menu.html" context_object_name = "pizza_menu" # ordering = ["price"] class PizzaDetailView(DetailView): model = PizzaMenu template_name = "pizza_detail.html" context_object_name = "pizza_detail" class About(TemplateView): template_name = "about.html"
urls.py
from pizza.views import IndexView, PizzaMenu, About, PizzaDetailView urlpatterns = [ path("", IndexView.as_view(), name="home"), path("menu/", PizzaMenu.as_view(), name="menu"), path("about/", About.as_view(), name="about"), path("pizza/<int:pk>/", PizzaDetailView.as_view(), name="pizza_detail"), path("admin/", admin.site.urls), ]
Advertisement
Answer
Don’t name your view PizzaMenu
, it will override the reference to the PizzaMenu
for the other views. Usually class-based views have a …View
suffix, so:
class IndexView(TemplateView): template_name = 'index.html' # add a View suffix to prevent collissions with the PizzaMenu model class class PizzaMenuListView(ListView): model = PizzaMenu template_name = 'menu.html' context_object_name = 'pizza_menu' class PizzaDetailView(DetailView): model = PizzaMenu template_name = 'pizza_detail.html' context_object_name = 'pizza_detail' class AboutView(TemplateView): template_name = 'about.html'