agl
agl

Reputation: 1

Django for loop in views.py only gets the first occurence

I am trying to display a different price for each item in a list, based on the logged-in user information.

However, my current code seems to overwrite the "price" key with the first occurrence "custom_price" ... I cannot seem to find a solution.

class MenuListView(ListView):
    model = Menu
    template_name = 'home.html'

    def get_context_data(self, **kwargs):
        if self.request.user.is_authenticated:
            context = super(MenuListView, self).get_context_data(**kwargs)
            user=get_object_or_404(Profile, user = self.request.user)
            coef = user.bmr
            menus = Menu.objects.all()
            price = []
            for menu in menus:
                menu_price = menu.price
                menu_kcal = menu.kcal
                custom_price = (menu_price/menu_kcal) * (coef*(35/100))
                price.append(custom_price)
            context['menus'] = menus
            context['prices'] = price
            return context

Template:

  {% for item in menus %}
            <div class="col-sm-6 col-md-6 col-lg-6">
                <div class="food-card food-card--vertical">
                    <div class="food-card_img">
                      {% for img in item.photo_set.all %}
                        <img src="{{ img.image.url }}" alt="">
                        {% endfor %}
                    </div>
                    <div class="food-card_content">
                        <div class="food-card_title-section">
                            <span  class="food-card_title">{{ item.title }}</span>
                            <span  class="food-card_author">{{ item.category }} - {{ item.focus }}</span>
                        </div>
                        <div class="food-card_bottom-section">
                            <div class="space-between">
                                <div>
                                    <span class="fa fa-fire"></span> {{ item.kcal }} Kcal
                                </div>

                            </div>
                            <hr>
                            <div class="space-between">
                                <div class="food-card_price">
                                          {% for p in prices %}
                                    <span>{{ p }} €</span>
                                          {% endfor %}
                                </div>

                            </div>
                        </div>
                    </div>
                </div>
            </div>

            {% endfor %} 

Could someone help ?

Many thanks in advance !

Upvotes: 0

Views: 665

Answers (1)

Italo Lemos
Italo Lemos

Reputation: 1022

You need to loop over your price list too. You're just looping over your menu list.

Each item on menu list has a price, and the price is on your price list. If you just use {{ price }} this display the entire list of prices and not a specific price for certain item.

EDIT: Just need iterate over the lists simultaneously since they have the same size.

A better way is zip both lists as here

menus_prices = zip(menus, prices)
context['menus_prices'] = menus_prices
return context

In templates you can do:

{% for item, price in menus_prices %}
            <div class="col-sm-6 col-md-6 col-lg-6">
                <div class="food-card food-card--vertical">
                    <div class="food-card_img">
                      {% for img in item.photo_set.all %}
                        <img src="{{ img.image.url }}" alt="">
                        {% endfor %}
                    </div>
                    <div class="food-card_content">
                        <div class="food-card_title-section">
                            <span  class="food-card_title">{{ item.title }}</span>
                            <span  class="food-card_author">{{ item.category }} - {{ item.focus }}</span>
                        </div>
                        <div class="food-card_bottom-section">
                            <div class="space-between">
                                <div>
                                    <span class="fa fa-fire"></span> {{ item.kcal }} Kcal
                                </div>

                            </div>
                            <hr>
                            <div class="space-between">
                                <div class="food-card_price">
                                          
                                    <span>{{ price }} €</span>
                                          
                                </div>

                            </div>
                        </div>
                    </div>
                </div>
            </div>

            {% endfor %} 

Upvotes: 1

Related Questions