英文:
Is there a way to filter multiple class objects in views.py and connect to each other?
问题
我有一个Django项目,我想要创建3个可以通过点击每个选择按钮来访问的菜单。
主菜单(如主食)>> 菜单(面食)>> 面食项目(bluh bluh)
- 主菜单可以从主页访问 *
我在models.py
中创建了3个类,分别是MainMenu
、Menu
和Item
:
class MainMenu(models.Model):
title = models.CharField(max_length=100)
main_slug = models.SlugField(max_length=50, unique=True)
created = models.DateTimeField("Date created", default=timezone.now)
def __str__(self):
return self.title
class Menu(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=50, unique=True)
created = models.DateTimeField("Date created", default=timezone.now)
main_menus = models.ForeignKey(MainMenu, default="", verbose_name="Menus", on_delete=models.SET_DEFAULT, null=True)
def __str__(self):
return f"{self.title}||{self.main_menus}"
class Meta:
ordering = ['-created']
class Item(models.Model):
title = models.CharField(max_length=100)
price = models.CharField(max_length=12)
description = models.TextField(blank=True)
created = models.DateTimeField("Date created", default=timezone.now)
menus = models.ForeignKey(Menu, default="", verbose_name="Menus", on_delete=models.SET_DEFAULT)
def __str__(self):
return f"{self.title}||{self.menus}"
class Meta:
ordering = ['-created']
然后在views.py
中过滤了对象:
def menus(request, main_slug):
matching_menus = Menu.objects.filter(main_menus__main_slug=main_slug).all()
return render(
request=request,
template_name='Menu.html',
context={"objects": matching_menus}
)
def items(request, slug):
matching_items = Item.objects.filter(menus__slug=slug).all()
return render(
request=request,
template_name='Item.html',
context={"objects": matching_items}
)
在urls.py
中配置路由:
urlpatterns = [
...
path('<slug:main_slug>', views.menus),
path('<str:slug>', views.items),
]
但是目前只有菜单被正确过滤,无法访问菜单中的项目。当点击"pastas"这样的菜单时,它加载一个空白的菜单HTML页面,并且不会显示任何错误。尝试连接三个菜单,但最终只连接了两个。
英文:
I have a Django project where I want to make 3 menus that can be accessed by clicking the select button in each
main_menu(like mainfood) >> menu(pastas) >> items of pastas(bluh bluh)
*main_menu is accessed from the homepage*
I have created 3 classes in models.py
called MainMenu
, Menu
, Item
class MainMenu(models.Model):
title = models.CharField(max_length=100)
main_slug = models.SlugField(max_length=50,unique=True)
created = models.DateTimeField("Date created", default=timezone.now)
def __str__(self):
return self.title
class Menu(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=50,unique=True)
created = models.DateTimeField("Date created", default=timezone.now)
main_menus = models.ForeignKey(MainMenu,default="",verbose_name="Menus",on_delete=models.SET_DEFAULT, null=True)
def __str__(self):
return f"{self.title}||{self.main_menus}"
class Meta:
ordering = ['-created']
class Item(models.Model):
title = models.CharField(max_length=100)
price = models.CharField(max_length=12)
description = models.TextField(blank=True)
created = models.DateTimeField("Date created", default=timezone.now)
menus = models.ForeignKey(Menu,default="",verbose_name="Menus",on_delete=models.SET_DEFAULT)
def __str__(self):
return f"{self.title}||{self.menus}"
class Meta:
ordering = ['-created']
and I have filtered objects in views.py
def menus(request,main_slug):
matching_menus = Menu.objects.filter(main_menus__main_slug=main_slug).all()
return render(
request=request,
template_name='Menu.html',
context={"objects": matching_menus}
)
def items(request,slug):
matching_items = Item.objects.filter(menus__slug=slug).all()
return render(
request=request,
template_name='Item.html',
context={"objects": matching_items}
)
and the urls.py
:
urlpatterns = [
...
path('<slug:main_slug>', views.menus),
path('<str:slug>', views.items),
]
it kinda WORKS but only menu is filtered correctly.
I can go from MainMenu to Menu and the Menus are filtered correctly,
but I CANNOT access the items in the Menus. when I click a menu like "pastas" it loads up a blank menu html and doesn't give any errors.
tried to connect three menus but ended up connecting only two.
答案1
得分: 0
这是由于它始终匹配菜单的 URL。在您的示例中,字符串 "pastas" 可以同时匹配作为 slug 和字符串。由于菜单的 URL 先列出,Django 会选择它。
要修复这个问题,可以为不同的页面使用不同的 URL,例如:
urlpatterns = [
...
path('main_menu/<slug:main_slug>', views.menus),
path('menu/<str:slug>', views.items),
]
英文:
It's because it's always matching the url for the menus. In your example, the string "pastas" can be matched both as a slug and as a string. Since the url for the menus is listed first, that is the one that is selected by Django.
To fix it, use different urls for the different pages, for example:
urlpatterns = [
...
path('main_menu/<slug:main_slug>', views.menus),
path('menu/<str:slug>', views.items),
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论