0

I try to get the data in my models "Machines" and then need to get Additional information form other table related to each machine. I try bellow code in views.py and render it to specified html page.

def allmachinesLOGO(request):
    machines=Machine.objects.all()
    c=""
    for m in machines:
        if m.tb_order_set.filter(status="2").exists():
            c="2"
        else:
            c="0"
    
    context ={'machines':machines,'condition':c}
    return render(request,'pline/machineslogos.html',context)
{% if condition == "2"  %}
            
                    <h4> working</h4>
                    <img class="btn-circle" style="width: 15px" src="{% static 'images/icons/icons8-green-circle-48.png' %}" alt="image" />
                  {% else %}
                      <h4>stop</h4>
                          {{ condition }}
                    <img class="btn-circle" style="width: 15px" src="{% static 'images/icons/icons8-red-circle-48.png' %}" alt="image" />
                    {% endif %}

what's the correct way to pass loop from views.py to template in Django

8
  • What exactly you want to display on template Commented Nov 11, 2022 at 7:11
  • hi i just want to show if the machine is working or not working by status filed from another table . if status =2 then is working if not then shows not working Commented Nov 11, 2022 at 7:31
  • in terminal when I print condition , its work fine but when I move html to check if is equal to "2" or not it always written 0 Commented Nov 11, 2022 at 7:40
  • what is the value of this line m.tb_order_set.filter(status="2").exists() ? Commented Nov 11, 2022 at 7:44
  • its placed in for loop , for each entry in table , if statement check if a row with status=2 is exist or not , base on that it written true or false. so if its true then c is equal to 2 and if is not c=0 Commented Nov 11, 2022 at 7:49

1 Answer 1

1

In the view, at each iteration of the loop, I create a dictionary with two values: 'machines', 'status' and put it in the aaa list.

Either remove the line in the view(if you don't want the lines with 'status'= '0' to be displayed.):

else:
     aaa.append({'machines': m, 'status': '0'})

then the line with 'machines': m, 'status': '0' will not be recorded.

Perhaps there is a way without a cycle to filter by the secondary model. It would be interesting to see if there is such a way.

views.py

def allmachinesLOGO(request):
    machines = Machine.objects.all()
    aaa = []
    for m in machines:
        if m.tb_order_set.filter(status='2').exists():
            aaa.append({'machines': m, 'status': '2'})
        else:
            aaa.append({'machines': m, 'status': '0'})

    context = {'context': aaa}

    return render(request, 'pline/machineslogos.html', context)

templates

{% for m in context %}
{% if m.status == '2' %}
<h4> working</h4>
<p>{{ 'machines' }} : {{ m.machines }} {{ 'status' }} : {{ m.status }} </p>
{% else %}
<h4>stop</h4>
<p>{{ 'machines' }} : {{ m.machines }} {{ 'status' }} : {{ m.status }} </p>
{% endif %}
{% endfor %}

One more way. Filter Machine by secondary model with status='2'. To make sure this is the case, two loops (the outer one iterates over Machine, and the inner one a.tb_order_set.all() and outputs status to print(stat.status)):

bbb = Machine.objects.filter(tb_order__status='2')#If unique records are needed, then add distinct().
print(bbb)
for a in bbb:
  for stat in a.tb_order_set.all():
     print(stat.status)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.