Home > Net >  How to hide entire row if one or two fields are empty Django
How to hide entire row if one or two fields are empty Django

Time:07-09

How can I hide an entire row if one or more specific fields are empty? For example, I have a django query set up so that I can get a total profit from items in the inventory manager. The way that I have that written is like:

html

{% extends 'portal/base.html' %}
{% block title %}Inventory{% endblock %}
{% block content %}
        <br>
        <div >
          <div >
           <form  role="search" action="/search" method="get">
              <input  type="text" name="q" placeholder="Search" aria-label="Search">
              <button >Search</button>
            </form>
          </div>
          <div >
           <a  href="/newitem" type="button">Input New Purchase</a>
          </div>
        </div>
      </div>
     <table >
       <thead>
           <tr>
            <th>Breakdown</th>
            <th>Product ID</th>
            <th>Product</th>
            <th>Total Profit</th>
           </tr>
       </thead>
       {% for inventory in inventory %}
       <tr>
        <td><a class='btn btn-success btn-sm' href=''>View Breakdown</a>
        <td>{{inventory.id}}</td>
        <td>{{inventory.product}}</td>
        <td>{{ inventory.Calculate_profit }}</td>
       </tr>
       {% endfor %}
    </table>
{% endblock %}

views.py

@login_required(login_url="/login")
def profitsperitem(request):
    inventory = Inventory.objects.all().order_by('id')

    return render(request, 'portal/profitsperitem.html', {"inventory": inventory})

models.py

@property
    def Calculate_profit(self):
        soldfor = Inventory.objects.filter(soldprice=self.soldprice).aggregate(Sum('soldprice'))['soldprice__sum'] or 0.00 
        paidfor = Inventory.objects.filter(paid=self.paid).aggregate(Sum('paid'))['paid__sum'] or 0.00 
        shipfor = Inventory.objects.filter(shipcost=self.shipcost).aggregate(Sum('shipcost'))['shipcost__sum'] or 0.00

        totalprofit = soldfor - paidfor - shipfor

        return totalprofit

As long as the model fields soldprice , paid , and shipcost are all filled out on every row in the database, I can get the results no problem. I get an error if soldprice or shipcost are null or none, so when there is nothing added to database. If one row does not have soldprice or shipcost set, none of the results can be viewed as this error pops up: "TypeError at /profitsperitem unsupported operand type(s) for -: 'float' and 'decimal.Decimal'"

My question is how can I hide the entire row if either or both soldprice and/or shipcost are empty?

CodePudding user response:

I was able to update the view like this

@login_required(login_url="/login")
def profitsperitem(request):
    inventory = Inventory.objects.filter(soldprice__isnull = False, shipcost__isnull = False).order_by('id')

    return render(request, 'portal/profitsperitem.html', {"inventory": inventory})

CodePudding user response:

    {% for inventory in inventory %}
        {% if inventory.Calculate_profit %}
           <tr>
            <td><a class='btn btn-success btn-sm' href=''>View Breakdown</a>
            <td>{{inventory.id}}</td>
            <td>{{inventory.product}}</td>
            <td>{{ inventory.Calculate_profit }}</td>
           </tr>
        {% endif %}
    {% endfor %}

you can print row only if you have calculate price.

if(soldfor is not None and paidfor is not None and shipfor is not None )
  totalprofit = soldfor - paidfor - shipfor
else totalprofit =None

Also You can check if Calculate profit has some value

  • Related