繁体   English   中英

如何在 Django 中显示小于特定数字的值

[英]How to display values that are less than a particular number in Django

我创建了一个名为 Product 的 model 包含这些字段('prod_name'、'company'、'quantity'、'price'、'units'、'prod_type')

我想在网页中显示剩余少于 2 个单位的产品我尝试使用 Model.Objects.filter(units__lte=2) 但我没有得到所需的 output

这是我的views.py文件:

from django.shortcuts import render, redirect, get_object_or_404
from django.views.generic import TemplateView, ListView
from django.db.models import Q
from .models import *
from .forms import *

def get_stock(request):
   items=Product.objects.filter(units__lte=2)

   context={
     'items':items
    }

   return render(request, 'UpdateStock.html', context)

这是我的网址文件:

from django.conf.urls import url
from django.urls import path
from .views import *

urlpatterns=[
   url(r'^get_stock$', get_stock, name='get_stock'),
]

这是我的 HTML 文件

{% extends 'base.html' %}

    {% block body %}

    <br>
        <h3>Update Stocks</h3>
    <br>

    <table class="table table-hover">
        <thead>
            <tr>
                <th>Sr. No.</th>
                <th>Product Name</th>
                <th>Company</th>
                <th>Quantity</th>
                <th>Price</th>
                <th>Units</th>
                <th>Product Type</th>
            </tr>
        </thead>
        <tbody>
            {% for item in object_list %}
            <tr>
                <td>{{item.pk}}</td>
                <td>{{item.prod_name}}</td>
                <td>{{item.company}}</td>
                <td>{{item.quantity}}</td>
                <td>{{item.price}}</td>
                <td>{{item.units}}</td>
                <td>{{item.prod_type}}</td>
            </tr>


            {% endfor %}

        </tbody>
    </table>    

    {% endblock %}

但是结果没有显示出来

我想在网页中显示剩余少于 2 个单位的产品,我尝试使用Model.objects.filter(units__lte=2)但我没有得到所需的 output。

__lte查找 [Django-doc]是一个过滤器,用于过滤“小于或等于”,这就是lte中的e的含义。 您可能需要使用__lt查找 [Django-doc]

Model.objects.filter(units__lt=2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM