我刚刚读到Eric Matthas的第18章(Django入门)的末尾。我在views.py中有下面的函数Pizza,它会引发下面提到的属性错误
AttributeError: 'Pizza' object has no attribute 'entry_set'
当我查看我的网站并单击特定比萨饼的链接时,会发生此错误。执行此操作应打开一个新视图(即网站上的一个页面),其中显示比萨饼的名称以及配料列表(即应执行下面的比萨饼功能)
def pizza(request,pizza_id):
"""Show the toppings for a pizza"""
pizza = Pizza.objects.get(id=pizza_id)
toppings = pizza.entry_set.order_by('name')
context = {'pizza': pizza, 'toppings': toppings}
return render(request, 'pizzeria/pizza.html', context)
下面是我在models.py上的课程。Pizza类创建Pizza对象
class Pizza(models.Model):
"""The name of a specific type of pizza."""
name = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
"""Return a string representation of the model."""
return self.name
class Topping(models.Model):
"""toppings that belong to specific types of pizzas"""
pizza = models.ForeignKey(Pizza, on_delete=models.CASCADE)
name = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
"""Return a string representation of the model."""
return self.name
我按照本书本章中的练习创建了一个Django项目,该项目允许我创建有关特定主题的日志条目。该期刊项目中的代码运行良好&;我或多或少地把那个练习中的代码“复制/复制”到我的比萨店项目中。我所做的唯一重大改变是,我想按名称而不是按项目中添加到Django数据库的日期对浇头进行排序
This question与我的非常相似,但没有解决我的问题
如果有什么我可以提供的帮助与故障排除,请让我知道
由于模型是} parameter [Django-doc]提供值,因此您可以使用
Topping
,并且您没有为^{topping_set
访问Topping
:相关问题 更多 >
编程相关推荐