类型对象 'PizzaMenu' 没有属性“_default_manager”

type object 'PizzaMenu' has no attribute '_default_manager'

我尝试访问模板上的 PizzaDetailView 时出现以下错误:/pizza/6/ 处的 AttributeError 类型对象 'PizzaMenu' 没有属性“_default_manager”。

问题出在哪里?

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),
]

不要 将您的视图命名为 PizzaMenu,它将覆盖其他视图对 PizzaMenu 的引用。通常 class-based 视图有一个 …View 后缀,所以:

class IndexView(TemplateView):
    template_name = 'index.html'


# add a View suffix to prevent collissions with the PizzaMenu model class
class <strong>PizzaMenuListView</strong>(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'