Victor Yee
Victor Yee

Reputation: 151

Issue with django rendering data onto template

I'm trying to render info on the html file, but info doesn't seem to be coming through correctly.

In the html below, it is supposed to render items that correspond with it's menu title.

For example

Title : Desserts

Item: Ice-cream

Description: Nice chocolate ice cream with sprinkles

$: 3.00


Title : Mains

Item: Curry

Description: Curry is hot

$: 10.00

The issue I'm facing is that all item in menuitems appears under all menu titles. The titles renders okay.

For example

Title : Desserts

Item: Ice-cream

Description: Nice chocolate ice cream with sprinkles

$: 3.00


Title : Mains

Item: Ice-cream

Description: Nice chocolate ice cream with sprinkles

$: 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 %}

How do I make sure the info shows up under the correct titles?

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)

Upvotes: 0

Views: 43

Answers (3)

Mangu Singh Rajpurohit
Mangu Singh Rajpurohit

Reputation: 11420

As @Geo Jacob has suggested, made those changes. Also in the view function, no need to set "menuitems" variable in the context dictionary.

Upvotes: 0

f43d65
f43d65

Reputation: 304

Try to remove menuitems from view (or just don't add menuitems to context) and replace {% for item in menuitems %} with {% for item in menu.menuitem_set.all %}.

Upvotes: 0

Geo Jacob
Geo Jacob

Reputation: 6009

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

Change this code to ,

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

Upvotes: 1

Related Questions