为什么结构化菜单无法在html模板中呈现?

问题描述 投票:0回答:1

我想在不使用mptt或django-tree的情况下为餐厅菜单建立结构数据库。这是我的models.py

from django.db import models
class Menu(models.Model):
    name = models.CharField(max_length=24, unique=True, verbose_name='menu name')
    #slug = models.SlugField(max_length=24, unique=True, help_text='The slug is the URL friendly version of the menu name, so that this can be accessed at a URL like mysite.com/menus/dinner/.')
    additional_text = models.CharField(max_length=128, null=True, blank=True, help_text='Any additional text that the menu might need, i.e. Served between 11:00am and 4:00pm.')
    order = models.PositiveSmallIntegerField(default=0, help_text='The order of the menu determines where this menu appears alongside other menus.')

    class Meta:
        ordering = ['name', 'order']

class MenuCategory(models.Model):
    menu = models.ForeignKey(Menu, on_delete=models.CASCADE,help_text='The menus that this category belongs to, i.e. \'Lunch\'.') 
    name = models.CharField(max_length=32, verbose_name='menu category name')
    additional_text = models.CharField(max_length=128, null=True, blank=True, help_text='The additional text is any bit of related information to go along with a menu category, i.e. the \'Pasta\' category might have details that say \'All entrees come with salad and bread\'.')
    order = models.IntegerField(default=0, help_text='The order is the order that this category should appear in when rendered on the templates.')

    class Meta:
        verbose_name='menu category'
        verbose_name_plural='menu categories'
        ordering = ['order', 'name']

    def __unicode__(self):
        return self.name

class MenuItem(models.Model):
    CLASSIFICATION_CHOICES = (
        ('neither', 'Neither'),
        ('vegan', 'Vegan'),
        ('vegetarian', 'Vegetarian'),
    )

    name = models.CharField(max_length=48, help_text='Name of the item on the menu.')
    description = models.CharField(max_length=128, null=True, blank=True, help_text='The description is a simple text description of the menu item.')
    category = models.ManyToManyField(MenuCategory, verbose_name='menu category', help_text='Category is the menu category that this menu item belongs to, i.e. \'Appetizers\'.')
    order = models.IntegerField(default=0, verbose_name='order', help_text='The order is to specify the order in which items show up on the menu.')
    price = models.IntegerField(help_text='The price is the cost of the item.')
    image = models.ImageField(upload_to='menu', null=True, blank=True, verbose_name='image', help_text='The image is an optional field that is associated with each menu item.')


    class Meta:
        verbose_name='menu item'
        verbose_name_plural='menu items'
        ordering = ['classification', 'order', 'name']

    def __unicode__(self):
        return self.name

views.py:

from .models import Menu,MenuCategory
from django.views.generic import ListView

class MenuView(ListView):
    model= Menu
    conext_object_name='name'
    template_name = 'menu_list.html'
    queryset = Menu.objects.all()
    def get_context_data(self, **kwargs):
        context = super(MenuView, self).get_context_data(**kwargs)
        context['Menucategory'] = MenuCategory.objects.all()
        context['Menus'] = self.queryset
        return context

menu_list.html:

<div>
{%for item in Menu%}
    <p>
        {{item.menu}}
        {{item.name}}
        {{item.additiona_text}}
        {{item.order}}
    </p>
    {%endfor%}
    {%for item in MenuCategory%}
    <p>
        {{item.name}}
        {{item.additiona_text}}
        {{item.order}}
    </p>
    {%endfor%}
</div>

当我执行runserver,后浏览本地主机页面时,它仅显示

Django博客

早餐1

午餐2

晚餐3

但是我想要的输出应该是:

1]早餐

咖啡

物品

1 2 3

零食1 2 3

2]午餐

入门项目1 2 3

主要课程项目1 2 3

我如何使用普通的引导表行列获取此结构化表,在每行列中我都会使用for循环查询项目?他们还有其他办法吗?如果有其他方法,我也有兴趣。...

python django django-models django-templates django-views
1个回答
0
投票

我认为您的设计一开始就不好。如果您想以这种方式显示事物,则逻辑如下:

  • 您有菜单类别(早餐,午餐,晚餐...),
  • 您有属于一个或多个菜单类别的菜单项(咖啡...),>
  • 您具有属于一个类别的菜单
  • 因此,您的数据结构应为以下内容:

class MenuItem(models.Model):
    # [...]

class MenuCategory(models.Model):
    # [...]
    name = CharField...
    items = models.ManyToManyField(MenuItem)

class Menu(models.Model):
    # [...]
    menu_category = models.ForeignKey(MenuCategory)

然后,您可以在模板中做您想做的事(note

:用其类名命名查询集是一种非常不好的做法。只需将其命名为menus!这就是我要做的此处),并带有一个双for循环:
<div>
{% for menu in menus %} 
    <h3>
        {{ menu.menu_category.name }}
    </h3>    
    <p>
        {%for item in menu.menu_category.items.all %}
            {{ item.name }}
        {% endfor %}
    </p>    
{% endfor %}
</div>

再次,您的初始设计在我看来不是最合适的。

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.