title: How to check two models ID whitout nested loop?
body: <p>My problem is the nested loop of my template... I use it to check if my model <code>sitio.id_sitio</code> has an equivalent ID with another model <code>comprobante.id_sitio</code> with a Foreign Key and then print A if one result is found and B if none</p>
  
  <p>The conditional if work fine but I don't want the nested loop print multiple times.</p>
  
  <p>If one result is found I want to break the cycle and print the HTML only one time, like <code>&lt;a href=""&gt; Checkouts &lt;/a&gt;</code> 
  Else if the result doesn't exist in the records at the end of the for I want to print <code>&lt;p&gt;No payments&lt;/p&gt;</code></p>
  
  <p>I don't know if I have to write a query in the views.py or if I have to do something else in the templates...
  Is there a correct way to do this?</p>
  
  <hr>
  
  <p>This is my code:</p>
  
  <p>Models.py</p>
  
  <pre><code>class Comprobante(models.Model):
          id_sitio = models.ForeignKey('Sitio', models.DO_NOTHING, db_column='id_sitio', blank=True, null=True)
  
  class Sitio(models.Model):
          id_sitio = models.IntegerField(primary_key=True)
          sitio = models.CharField(max_length=100, blank=True, null=True)
  
          def __str__(self):
              return self.sitio
  </code></pre>
  
  <p>Views.py</p>
  
  <pre><code>def topsitios(request):
      sitio = Sitio.objects.all()[0:100]
      comprobante = Comprobante.objects.all()[0:100]
  
      context = {'sitio': sitio, 'comprobante': comprobante}
  
      return render(request, "sitio_ptc/topsitios.html", context)
  </code></pre>
  
  <p>Template.html</p>
  
  <pre><code>{% block content %}
  
  {% for s in sitio %}
  &lt;tr&gt;
  &lt;th scope="row"&gt;  {{ forloop.counter }}&lt;/th&gt;
  &lt;td&gt; {{ s.sitio }} &lt;/td&gt;
  
  &lt;td&gt;
  {% for c in comprobante %}
  {% if s.id_sitio == c.id_sitio_id %}
  &lt;a href=""&gt;Checkouts&lt;/a&gt;
  {% else %}
  &lt;p&gt;no payments&lt;/p&gt;
  {% endif %}
  {% endfor %}
  &lt;/td&gt; 
  
  &lt;/tr&gt; 
  {% endfor %}
  {% endblock %}
  </code></pre>