django将数据渲染到模板

时间:2015-07-27 06:07:27

标签: django django-models django-templates django-admin django-views

我试图在html文件上呈现信息,但信息似乎没有正确显示。

在下面的html中,它应该呈现与其菜单标题相对应的项目。

例如

  

标题:甜点

     

项目:冰淇淋

     

描述:漂亮的巧克力冰淇淋洒上

     

$:3.00

           

标题:主要

     

项目:咖喱

     

描述:咖喱很热

     

$:10.00

我面临的问题是菜单项中的所有项目都会显示在所有菜单标题下。标题渲染没问题。

例如

  

标题:甜点

     

项目:冰淇淋

     

描述:漂亮的巧克力冰淇淋洒上

     

$:3.00

  

标题:主要

     

项目:冰淇淋

     

描述:漂亮的巧克力冰淇淋洒上

     

$:3.00

HTML

    {% for menu in menus %}
    <section id="services">
    <div class="container">
        <div class="row">
            <div class="col-lg-12 text-center">
                <h2 class="section-heading">Title {{ menu.title }}</h2>
                <hr class="primary">
            </div>
        </div>
    </div>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 text-center">
                {% for item in menuitems %}
                    <p>Item {{ item.item_name }}</p>
                    <p>Description {{ item.description }}</p>
                    <p>$ {{ item.price }}</p>
                {% endfor %}
            </div>
        </div>
    </div>
</section>    

    {% endfor %}

如何确保信息显示在正确的标题下?

views.py

def single(request, slug):
    user = request.user
    restaurant = Restaurant.objects.get(slug=slug)
    template = 'restaurants/single.html'
    menus = restaurant.menutitle_set.all()

    menuitems = MenuItem.objects.filter(title=menus)



    context = {
            'restaurant': restaurant,
            'menus': menus,
            'menuitems': menuitems,
    }
    return render(request, template, context)

models.py

class Restaurant(models.Model):
    user = models.CharField(max_length=250)
    restaurant_name = models.CharField(max_length=250)
    restaurant_address1 = models.CharField(max_length=250)
    restaurant_address2 = models.CharField(max_length=250)
    restaurant_state = models.CharField(max_length=120, choices=STATE_CHOICES)
    slug = models.SlugField(unique=True)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __unicode__(self):
        return self.restaurant_name

    def get_absolute_url(self):
        return reverse("single_restaurant", kwargs={"slug": self.slug})


class MenuTitle(models.Model):
    restaurant = models.ForeignKey(Restaurant)
    title = models.CharField(max_length=120)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)
    active = models.BooleanField(default=True)

    def __unicode__(self):
        return self.title


class MenuItem(models.Model):
    title = models.ForeignKey(MenuTitle)
    item_name = models.CharField(max_length=2000)
    description = MarkdownField(max_length=2000, null=True, blank=True)
    price = models.CharField(max_length=2000)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)
    active = models.BooleanField(default=True)

    def __unicode__(self):
        return self.item_name

admin.py

class MenuTitleInline(admin.StackedInline):
    model = MenuTitle
    extra = 1

class MenuItemInline(admin.StackedInline):
    model = MenuItem
    extra = 1

class RestaurantAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("restaurant_name",)}

    class Meta:
        model = Restaurant

    inlines = [MenuTitleInline]

admin.site.register(Restaurant, RestaurantAdmin)


class MenuTitleAdmin(admin.ModelAdmin):
    list_display = ["restaurant", "title"]
    class Meta:
        model = MenuTitle
    inlines = [MenuItemInline]

admin.site.register(MenuTitle, MenuTitleAdmin)

3 个答案:

答案 0 :(得分:1)

{% for item in menuitems %}
     <p>Item {{ item.item_name }}</p>
     <p>Description {{ item.description }}</p>
     <p>$ {{ item.price }}</p>
{% endfor %}

将此代码更改为,

{% for item in menu.menuitem.all %}
   <p>Item {{ item.item_name }}</p>
   <p>Description {{ item.description }}</p>
   <p>$ {{ item.price }}</p>
{% endfor %}

答案 1 :(得分:0)

尝试从视图中删除menuitems(或者只是不要将menuitems添加到上下文中)并将{% for item in menuitems %}替换为{% for item in menu.menuitem_set.all %}

答案 2 :(得分:0)

正如@Geo Jacob建议的那样,做出了这些改变。同样在视图函数中,无需在上下文字典中设置“menuitems”变量。