0
votes

This is a continuation of this post in which I use django-formset and detailview.

Let's see a customer orders an item on June 3rd. How can I retrieve the latest price of that item before June 3rd, which is $2?

enter image description here

I have the following django template tags, but it returns $4.

{% for cartitem_ in object.model_customercartitem_set.all %}
    {% for pricerow_ in cart_item.item_name.model_itemprice_set.all %}
        {% if price_row.timestamp|date:"U" <= object.timestamp|date:"U" %}
            {% if forloop.first %}
                {{ price_row.item_price }}
            {% endif %}
        {% endif %}
    {% endfor %}
{% endfor %}

Perhaps I have used forloop.first tag incorrectly, so what should the right tag be? Any other solution besides template tag works too.

Thanks

1
I think you should encode this logic in the view not in a template. It is a common misconception that a template should be used to calculate things. A template should actually only be used to render things in a nice way. - Willem Van Onsem
Thanks. I have tried to encode this in views, but to no avail when django-formset is involved. - Fxs7576

1 Answers

0
votes

You missed an {% endif %}

{% for cartitem_ in object.model_customercartitem_set.all %}
    {% for pricerow_ in cart_item.item_name.model_itemprice_set.all %}
        {% if price_row.timestamp|date:"U" <= object.timestamp|date:"U" %}
            {% if forloop.first %}
                {{ price_row.item_price }}
            {% endif %}
        {% endif %} <!-- here -->
    {% endfor %}
{% endfor %}