在views.py中是否有一种方法可以筛选多个类对象并将它们连接在一起?

huangapple go评论63阅读模式
英文:

Is there a way to filter multiple class objects in views.py and connect to each other?

问题

我有一个Django项目,我想要创建3个可以通过点击每个选择按钮来访问的菜单。

主菜单(如主食)>> 菜单(面食)>> 面食项目(bluh bluh)

  • 主菜单可以从主页访问 *

我在models.py中创建了3个类,分别是MainMenuMenuItem

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) &gt;&gt; menu(pastas) &gt;&gt; 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(&quot;Date created&quot;, 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(&quot;Date created&quot;, default=timezone.now)
    main_menus = models.ForeignKey(MainMenu,default=&quot;&quot;,verbose_name=&quot;Menus&quot;,on_delete=models.SET_DEFAULT, null=True)

    def __str__(self):
        return f&quot;{self.title}||{self.main_menus}&quot;    
    class Meta:
        ordering = [&#39;-created&#39;]


class Item(models.Model):
    title = models.CharField(max_length=100)
    price = models.CharField(max_length=12)
    description = models.TextField(blank=True)
    created = models.DateTimeField(&quot;Date created&quot;, default=timezone.now)
    menus = models.ForeignKey(Menu,default=&quot;&quot;,verbose_name=&quot;Menus&quot;,on_delete=models.SET_DEFAULT)

    def __str__(self):
        return f&quot;{self.title}||{self.menus}&quot;
    
    class Meta:
        ordering = [&#39;-created&#39;]

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=&#39;Menu.html&#39;,
        context={&quot;objects&quot;: matching_menus}
        )

def items(request,slug):
    matching_items = Item.objects.filter(menus__slug=slug).all()
    return render(
        request=request,
        template_name=&#39;Item.html&#39;,
        context={&quot;objects&quot;: matching_items}
        )

and the urls.py :

urlpatterns = [
...
    path(&#39;&lt;slug:main_slug&gt;&#39;, views.menus),
    path(&#39;&lt;str:slug&gt;&#39;, 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(&#39;main_menu/&lt;slug:main_slug&gt;&#39;, views.menus),
    path(&#39;menu/&lt;str:slug&gt;&#39;, views.items),
]

huangapple
  • 本文由 发表于 2023年6月6日 04:59:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定