0

I am trying to check if the date an order was created is still less than 5 days, then i want to display a New Order Text.

This is how i have tried doing this

def vendor_orders(request):
    five_days = datetime.now() + timedelta(days=5)
    new_orders = CartOrder.objects.filter(payment_status="paid", date__lte=five_days).order_by("-id")

This is returning all the orders in the database that the date is less than 5 days, which means all orders both old and new are being returned and this is not what i am expecting. If i manually create an order and set the date manually to some date in the future e.g July or August, it does not return that order.

Please how can i go about this logic? all i want to do is display an order as new orders if the date which the order was created is not yet older than 5 days.

Template Update

i want to check if the order is a new order then i display a new badge

Views.py


@login_required
def vendor_orders(request):
    five_days = datetime.now() - timedelta(days=5) # this is 5 days ago
    new_order = CartOrder.objects.filter(payment_status="paid", vendor=request.user.vendor, date__gte=five_days).order_by("-id")

    order = CartOrder.objects.filter(payment_status="paid", vendor=request.user.vendor).order_by("-id")
    
    context = {
        "order":order,
        "five_days":five_days,
        "new_order":new_order,
    } 
    return render(request, "vendor/vendor-order.html", context)

{% for o in order %}
<tr>
   <th scope="row">#{{o.oid}} <span class="badge">{% if o.date > five_days %}New{% endif %}</span></th>
   <th scope="row"><a href="{% url 'vendor:order-detail' o.oid %}" class="btn btn-primary">View Order</a></th>
   <td>{{o.date}}</td>
</tr>
{% endfor %}

1 Answers1

1

If I understand correctly, I think this is what you want.

def vendor_orders(request):
    five_days = datetime.now() - timedelta(days=5) #5 days ago
    
    #orders with a date greater than the date 5 days ago
    new_orders = CartOrder.objects.filter(payment_status="paid", date__gte=five_days).order_by("-id") 
lucutzu33
  • 3,470
  • 1
  • 10
  • 24
  • Yes, that is it. ,Please help me look at this conditional statement i wrote in the template for the same logic, `{% if o.date > five_days %}New{% endif %}` it seems not to be working. I have updated the question with my updated view and template - – Destiny Franks Apr 06 '23 at 16:19
  • Then why did you need to do a query for new orders if you want to do the filtering in the template anyway? – lucutzu33 Apr 06 '23 at 16:26
  • that is because i need to count how many new orders and also display the result in another section in the template like this `Total New Orders - {{ new_orders.count }}` – Destiny Franks Apr 06 '23 at 16:28
  • 1
    Combining my answer with the answers in this thread should help you: https://stackoverflow.com/questions/3798812/how-to-compare-dates-in-django-templates – lucutzu33 Apr 06 '23 at 16:33