0

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 !

3
  • How do you render this in the template? Commented Dec 22, 2020 at 18:39
  • Hi Willem, I edited the post. Commented Dec 22, 2020 at 18:43
  • Are you doing something different with price in the template earlier? In your view, you set price to be a list, but the template looks like it's a single value. That seems like a discrepancy to me. Commented Dec 22, 2020 at 18:49

1 Answer 1

1

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 %} 
Sign up to request clarification or add additional context in comments.

2 Comments

Indeed thank you ! But it does not solve the main issue which is the fact that, in the list, I only get the custom price of the first menu item.
@agl Check my answer now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.