如何在Django查询中检查日期是否仍然少于5天?

huangapple go评论101阅读模式
英文:

How to check if a date is still less than 5 days in django query?

问题

我尝试检查订单创建日期是否仍然少于5天,然后我想显示一个新订单文本。

这是我尝试实现这一点的方式:

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

这会返回数据库中所有日期少于5天的订单,这意味着所有订单,包括旧订单和新订单,都会被返回,这不是我期望的结果。如果我手动创建一个订单并将日期手动设置为未来的某个日期,比如七月或八月,它不会返回该订单。

请问我该如何处理这个逻辑?我想要的只是如果订单创建日期还没有超过5天,就将其显示为新订单。

模板更新:
我想要检查订单是否是新订单,然后显示一个徽章。

Views.py:

  1. @login_required
  2. def vendor_orders(request):
  3. five_days = datetime.now() - timedelta(days=5) # 这是5天前
  4. new_order = CartOrder.objects.filter(payment_status="paid", vendor=request.user.vendor, date__gte=five_days).order_by("-id")
  5. order = CartOrder.objects.filter(payment_status="paid", vendor=request.user.vendor).order_by("-id")
  6. context = {
  7. "order": order,
  8. "five_days": five_days,
  9. "new_order": new_order,
  10. }
  11. return render(request, "vendor/vendor-order.html", context)
  1. {% for o in order %}
  2. <tr>
  3. <th scope="row">#{{o.oid}} <span class="badge">{% if o.date > five_days %}New{% endif %}</span></th>
  4. <th scope="row"><a href="{% url 'vendor:order-detail' o.oid %}" class="btn btn-primary">View Order</a></th>
  5. <td>{{o.date}}</td>
  6. </tr>
  7. {% endfor %}
英文:

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

  1. def vendor_orders(request):
  2. five_days = datetime.now() + timedelta(days=5)
  3. new_orders = CartOrder.objects.filter(payment_status=&quot;paid&quot;, date__lte=five_days).order_by(&quot;-id&quot;)

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

  1. @login_required
  2. def vendor_orders(request):
  3. five_days = datetime.now() - timedelta(days=5) # this is 5 days ago
  4. new_order = CartOrder.objects.filter(payment_status=&quot;paid&quot;, vendor=request.user.vendor, date__gte=five_days).order_by(&quot;-id&quot;)
  5. order = CartOrder.objects.filter(payment_status=&quot;paid&quot;, vendor=request.user.vendor).order_by(&quot;-id&quot;)
  6. context = {
  7. &quot;order&quot;:order,
  8. &quot;five_days&quot;:five_days,
  9. &quot;new_order&quot;:new_order,
  10. }
  11. return render(request, &quot;vendor/vendor-order.html&quot;, context)
  1. {% for o in order %}
  2. &lt;tr&gt;
  3. &lt;th scope=&quot;row&quot;&gt;#{{o.oid}} &lt;span class=&quot;badge&quot;&gt;{% if o.date &gt; five_days %}New{% endif %}&lt;/span&gt;&lt;/th&gt;
  4. &lt;th scope=&quot;row&quot;&gt;&lt;a href=&quot;{% url &#39;vendor:order-detail&#39; o.oid %}&quot; class=&quot;btn btn-primary&quot;&gt;View Order&lt;/a&gt;&lt;/th&gt;
  5. &lt;td&gt;{{o.date}}&lt;/td&gt;
  6. &lt;/tr&gt;
  7. {% endfor %}

答案1

得分: 1

如果我理解正确,我认为这是你想要的。

  1. def vendor_orders(request):
  2. five_days = datetime.now() - timedelta(days=5) #5天前的日期
  3. #日期大于5天前的订单
  4. new_orders = CartOrder.objects.filter(payment_status="paid", date__gte=five_days).order_by("-id")
英文:

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

  1. def vendor_orders(request):
  2. five_days = datetime.now() - timedelta(days=5) #5 days ago
  3. #orders with a date greater than the date 5 days ago
  4. new_orders = CartOrder.objects.filter(payment_status=&quot;paid&quot;, date__gte=five_days).order_by(&quot;-id&quot;)

huangapple
  • 本文由 发表于 2023年4月6日 23:53:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定