title: How to compare two querysets in Django templating?
body: <p>This might sound super similar to other postings about comparing two querysets using <code>zip</code> and other choices, but I have been reading lots and none of them gave me a good idea what I can do for the comparing I need.</p>
  
  <p>To start off, I am getting queries from the database and in the HTML templating I need to generate some defaults for <code>select</code> element. With one default it is easy, but with <code>multiple</code> I am having trouble since there's no <code>break</code> in Django templating.</p>
  
  <p>Let's say I have these two querysets that I get returned from the backend:</p>
  
  <pre><code>[&lt;J: j1&gt;, &lt;J: j2&gt;, &lt;J: j3&gt;, &lt;J: j4&gt;]
  [&lt;J: j2&gt;, &lt;J: j4&gt;]
  </code></pre>
  
  <p>I tried something like this to start with, and I can understand that it's looping more than needed, so I am getting more outputs than needed too. I tried searching for something that would break the loop, but I figured there's no breaking up the loop in a Django template.</p>
  
  <pre><code>&lt;select name="" id=""&gt;
      {% for j in all_j %}
          {% for s in all_s %}
              {% if j.id == s.id %}
                  &lt;option value="{{ j.id }}" selected="selected"&gt;{{ j.name }}&lt;/option&gt;
              {% else %}
                  &lt;option value="{{ j.id }}"&gt;{{ j.name }}&lt;/option&gt;
              {% endif %}
          {% endfor %}
      {% endfor %}
  &lt;/select&gt;
  </code></pre>
  
  <p>I tried other ways such as</p>
  
  <pre><code>&lt;select name="" id=""&gt;
      {% for s in all_s %}
          {% if s in all_j %}
              &lt;option value="{{ j.id }}" selected="selected"&gt;{{ j.name }}&lt;/option&gt;
          {% else %}
              &lt;option value="{{ j.id }}"&gt;{{ j.name }}&lt;/option&gt;
          {% endif %}
      {% endfor %}
  &lt;/select&gt;
  </code></pre>
  
  <p>Then I figured it only gets output if it's true, that things in <code>else</code> will not run.</p>
  
  <p>Can someone give me an idea or know what I can do with this kind of situation?</p>
  
  <p>below is what I am trying to achieve.</p>
  
  <p><a href="https://i.stack.imgur.com/a0PGb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/a0PGb.png" alt="enter image description here"></a></p>