title: Nested for loop in html using django to display information from 2 different models
body: <p>I have a model called Section and a model called SectionInfo. Section has one field and that's name. A user will create a section by giving it a name. SectionInfo has a foreign key section_name which links it to the Section model. I have a few other fields inside SectionInfo such as things like detail, date_created, etc. What I am trying to accomplish here is using a for loop to display section names that have already been created inside or above some cards that I have set up in an html template. Then I want to use a nested loop to gather the data that is tied to that section name which is what the user inputs in for the SectionInfo model. I am able to display the section names correctly, but the issue im having is in the loop inside that loop. The same detail link is being displayed in every section that was made. Information should only be displayed in the chosen section for which it was made.</p>
  <p>Here is a bit of code to help you understand what I am trying to accomplish.</p>
  <p><code>Template file:</code></p>
  <pre class="lang-html prettyprint-override"><code>{% extends &quot;base.html&quot; %}
  {% load bootstrap5 %}
  {% load crispy_forms_tags %}
  {% load static %}
  
  {% block content %}
  {% for i in object %}
  {{ i.name}}
  &lt;div class=&quot;row&quot;&gt;
      &lt;div class=&quot;col-sm-4&quot;&gt;
  
          &lt;div class=&quot;card&quot;&gt;
              &lt;div class=&quot;card-body&quot;&gt;
                  
              {% for ii in object_2 %}
                  
                  &lt;a href=&quot;{% url 'manufacturing:section_info_detail' i.id %}&quot;&gt;Details&lt;/a&gt;
                  
                  {% endfor %}
                  
                  
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  {% endfor %}
  {% endblock content %}
  </code></pre>
  <p>Here is what I am getting displayed
  <a href="https://i.stack.imgur.com/EPNSw.png" rel="nofollow noreferrer">whats being displayed</a></p>
  <p>Here is the view that's being used</p>
  <pre><code>def section_info_detail(request, pk):
      object = get_object_or_404(Section, id=pk)
      object_2 = get_object_or_404(SectionInfo, id=pk)
      context = {
          'object': object,
          'object_2':object_2,
      }
      return render(request, 'manufacturing/section_info_detail.html', context)
  </code></pre>
  <p><code>Models.py:</code></p>
  <pre class="lang-py prettyprint-override"><code>class Section(models.Model):
      name = models.CharField(max_length=30)
          
      def __str__(self):
          return self.name
  
  class SectionInfo(models.Model):
      section_name = models.ForeignKey(Section, on_delete=models.CASCADE)
      detail = models.CharField(max_length=50)
      text = models.TextField(max_length=2000)
      date_created = models.DateField('Date created', auto_now_add=True)
      date_updated = models.DateField('Date updated', auto_now=True)
  </code></pre>
  <p><code>Forms.py:</code></p>
  <pre class="lang-py prettyprint-override"><code>class SectionNameForm(forms.ModelForm):
      class Meta:
          model = Section 
          fields = [
              'name',
          ]
  
  class SectionInfoForm(forms.ModelForm):
      class Meta:
          model = SectionInfo 
          fields = [
              'section_name',
              'detail',
              'text',
          ]
  </code></pre>
  <p>If any other information is needed, do tell.</p>