# HTML for loop gives all values

**URL:** <https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846>\
**Category:** Templates & Frontend\
**Created:** [October 27, 2023, 10:09pm UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846 "2023-10-27T22:09:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jslovern391](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jslovern391/32/16283_2.png) [@jslovern391](https://forum.djangoproject.com/u/jslovern391)\
**Post date:** [October 27, 2023, 10:09pm UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846/1 "2023-10-27T22:09:55Z")

</div>

I want to get information from another table to show in a template. It should only show one Org Name - Jeff Group

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/3/5/3584a4a90f890c0ea1297f410bcfe9c022dc8a5f.png)

template

```auto
 <tbody>
            {% for org in org %}
            <tr>
                <td>{{ org.alias_name }}</td>
                <td>
                  {% for orgs in orgs %}
                    {{ orgs.org_name }}
                  {% endfor %}
                </td>

```

Let me know if you need more info.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 27, 2023, 10:23pm UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846/2 "2023-10-27T22:23:24Z")

</div>

First, I’d avoid reusing names within your `for` loops. I strongly encourage you to identify the sets as plural and the instances as singular.

Beyond that, we’d need to see your view to see the context that you are supplying to the template.

---

<div class="post-metadata">

**Author:** ![jslovern391](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jslovern391/32/16283_2.png) [@jslovern391](https://forum.djangoproject.com/u/jslovern391)\
**Post date:** [October 27, 2023, 10:35pm UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846/3 "2023-10-27T22:35:44Z")

</div>

```auto
login_required
def org_list(request):
    org = CustomUser.objects.all().filter(sponsor=request.user.pk).values().filter(status='oo')
    orgs = Organization.objects.all()

    context = {
        'org': org,
        'orgs': orgs,
        'sponsor': request.user.pk,
    }
    return render(request, 'accounts/org_list.html', context)

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 27, 2023, 10:40pm UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846/4 "2023-10-27T22:40:10Z")

</div>

If you only want to render one `Organization` object, why are you querying all of them?

What determines which `Organization` object that you want to render? That’s what you should be querying for.

---

<div class="post-metadata">

**Author:** ![jslovern391](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jslovern391/32/16283_2.png) [@jslovern391](https://forum.djangoproject.com/u/jslovern391)\
**Post date:** [October 27, 2023, 10:44pm UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846/5 "2023-10-27T22:44:42Z")

</div>

org

```auto
class CustomUser(AbstractUser):
    alias_email = models.EmailField(null=True, blank=True)
    alias_name = models.CharField(max_length=20, unique=True, null=True, blank=True)
    cell = models.CharField(blank=True, max_length=10, help_text='Cell phone number (numbers only)')
    downline = models.CharField(tuple, max_length=1000000, default=1)
    email_confirmed = models.BooleanField(default=False)
    org_agreement_accept = models.BooleanField(default=False)
    sponsor = models.BigIntegerField(default=1)
    sponsor_accept = models.BooleanField(default=False, null=True, blank=True)
    upline = models.CharField(tuple, max_length=1000000, default=1)
    org = models.ForeignKey(Organization, on_delete=models.SET_NULL, null=True)

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 27, 2023, 10:51pm UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846/6 "2023-10-27T22:51:37Z")

</div>

Ok. So you can remove the `Organization` query completely, along with the reference to it in your context.

If you have an instance of `CustomUser` called `custom_user`, then the related `Organization` is `custom_user.org`.

---

<div class="post-metadata">

**Author:** ![jslovern391](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jslovern391/32/16283_2.png) [@jslovern391](https://forum.djangoproject.com/u/jslovern391)\
**Post date:** [October 27, 2023, 11:02pm UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846/7 "2023-10-27T23:02:18Z")

</div>

Org Name does not show now.

![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/d/5/d5875fed0543faad7b2485cfe5aaabc73e45561f.png)

```auto
 <td>{{ org.alias_name }}</td>
                <td>{{ org.org }}</td>

```

@login\_required  
def org\_list(request):  
org = CustomUser.objects.all().filter(sponsor=request.user.pk).values().filter(status=‘oo’)  
# orgs = Organization.objects.all()

```
context = {
    'org': org,
    # 'orgs': orgs,
    'sponsor': request.user.pk,
}
return render(request, 'accounts/org_list.html', context)

```

```auto

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 27, 2023, 11:12pm UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846/8 "2023-10-27T23:12:07Z")

</div>

`org.org` is the reference to the `Organization` object. If you want an attribute of that object, that’s what you need to render.

---

<div class="post-metadata">

**Author:** ![jslovern391](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jslovern391/32/16283_2.png) [@jslovern391](https://forum.djangoproject.com/u/jslovern391)\
**Post date:** [October 27, 2023, 11:27pm UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846/9 "2023-10-27T23:27:03Z")

</div>

I have not done that before, so something like this?

```auto
def org_list(request):
    org = CustomUser.objects.all().filter(sponsor=request.user.pk).values().filter(status='oo')
    orgs = Organization.objects.all()

    context = {
        'org': org,
        'sponsor': request.user.pk,
    }
    return render(request, 'accounts/org_list.html', context, orgs)

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 27, 2023, 11:36pm UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846/10 "2023-10-27T23:36:51Z")

</div>

Let’s say you have an instance of `Organization` named `organization` - perhaps retrieved as:  
`organization = Organization.objects.get(id=1)`.

How would you reference the `name` attribute of `organization`? (not necessarily in a template)

To phrase it another way, finish this python statement:  
`org_name = `  
(What would you put on the right side of the equal sign to reference the the name?)

---

<div class="post-metadata">

**Author:** ![jslovern391](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jslovern391/32/16283_2.png) [@jslovern391](https://forum.djangoproject.com/u/jslovern391)\
**Post date:** [October 28, 2023, 12:08am UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846/11 "2023-10-28T00:08:37Z")

</div>

I would think it would be organization.org\_name

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 28, 2023, 1:00am UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846/12 "2023-10-28T01:00:21Z")

</div>

Yes.

So, if you have a reference to the same instance of `Organization` by the name `current_user.org`, how do you think you might reference the `org_name` attribute?

---

<div class="post-metadata">

**Author:** ![jslovern391](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jslovern391/32/16283_2.png) [@jslovern391](https://forum.djangoproject.com/u/jslovern391)\
**Post date:** [October 28, 2023, 1:03am UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846/13 "2023-10-28T01:03:14Z")

</div>

That is where I am stuck. I can get the org value which is the key for the Organizations table key, but I can’t figure out how to get it to render the name.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 28, 2023, 1:06am UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846/14 "2023-10-28T01:06:20Z")

</div>

Take a guess.

If you have a reference to an instance of `Organization` named `organization`, and you reference the `org_name` attribute as `organization.org_name`, what do you think the syntax might be if the name is `current_user.org` instead of `organization`?

---

<div class="post-metadata">

**Author:** ![jslovern391](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jslovern391/32/16283_2.png) [@jslovern391](https://forum.djangoproject.com/u/jslovern391)\
**Post date:** [October 28, 2023, 1:15am UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846/15 "2023-10-28T01:15:58Z")

</div>

tried

org.org\_name  
org.organization.org\_name  
organization.org\_name

I think I am overthinking this.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 28, 2023, 1:17am UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846/16 "2023-10-28T01:17:43Z")

</div>

Yes, I think you are.

If you have a reference to an instance of `Organization` named `organization`, then the name is referenced as `organization.org_name`.

If your name for the reference to the same instance of `Organization` is `current_user.org`, then the reference to the name would be `current_user.org.org_name`.

(And please, do yourself a favor and get rid of the name `org` being used for an instance of `CustomUser`.)

---

<div class="post-metadata">

**Author:** ![jslovern391](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jslovern391/32/16283_2.png) [@jslovern391](https://forum.djangoproject.com/u/jslovern391)\
**Post date:** [October 28, 2023, 3:24am UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846/17 "2023-10-28T03:24:01Z")

</div>

So if I understand this it should be and if so it is not displaying.

```auto
        <tbody>
          {% for fisher in fisher %}
            <tr>
                <td>{{ fisher.alias_name }}</td>
                <td>{{ fisher.org.org_name }}</td>
                <td>     

```

```auto
def org_list(request):
    fisher = CustomUser.objects.all().filter(sponsor=request.user.pk).values().filter(status=7)

    context = {
        'fisher': fisher,
        'sponsor': request.user.pk,
    }
    return render(request, 'accounts/org_list.html', context)

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 28, 2023, 4:01am UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846/18 "2023-10-28T04:01:48Z")

</div>

I would verify the data and queries by printing `fisher` in the view before the context definition and rendering `fisher` in the template inside the loop. (And again, it’s really a bad practice to try and reuse the same variable name for both the loop identifier and the loop variable. Your context variable should logically be `fishers` instead of `fisher`.)

It may also help if you posted the `Organization` model

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 28, 2023, 11:47am UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846/19 "2023-10-28T11:47:08Z")

</div>

Side note to the previous response - what you’re really trying to verify here is that you actually have the relationship established between `CurrentUser` and `Organization`, that the `org` attribute is referencing the `Organization` that you think it does, and that that `Organization` has the `org_name` field you’re expecting it to have and is not blank or null.

---

<div class="post-metadata">

**Author:** ![jslovern391](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jslovern391/32/16283_2.png) [@jslovern391](https://forum.djangoproject.com/u/jslovern391)\
**Post date:** [October 29, 2023, 4:57pm UTC](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846/20 "2023-10-29T16:57:14Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/a/b/abe780c42cdc5f046bf8f1fa1021832c66f544e3.png)

Those ids are correct in the db for org.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/4/7/47057e139d33e6035b271fa353795c57ce531497.png)

models

```auto
class Organization(models.Model):
    m28t_approved = models.BooleanField(default=False)
    org_name = models.CharField(max_length=50, unique=True)
    org_image = models.ImageField(upload_to='org/images/', null=True, blank=True)
    org_owner = models.BigIntegerField(unique=True, null=True, blank=True)
    org_address = models.CharField(max_length=50, null=True, blank=True)
    org_city = models.CharField(max_length=50, null=True, blank=True)
    org_state = models.CharField(max_length=2, null=True, blank=True)
    org_zip = models.CharField(max_length=5, null=True, blank=True)
    org_phone = models.CharField(max_length=10, null=True, blank=True)
    org_web = models.URLField(null=True, blank=True)

    def __str__ (self):
        return self.org_name

class CustomUser(AbstractUser):
    alias_email = models.EmailField(null=True, blank=True)
    alias_name = models.CharField(max_length=20, unique=True, null=True, blank=True)
    cell = models.CharField(blank=True, max_length=10, help_text='Cell phone number (numbers only)')
    downline = models.CharField(tuple, max_length=1000000, default=1)
    email_confirmed = models.BooleanField(default=False)
    org_agreement_accept = models.BooleanField(default=False)
    sponsor = models.BigIntegerField(default=1)
    sponsor_accept = models.BooleanField(default=False, null=True, blank=True)
    upline = models.CharField(tuple, max_length=1000000, default=1)
    org = models.ForeignKey(Organization, on_delete=models.SET_NULL, null=True)

```

[Next page](https://forum.djangoproject.com/t/html-for-loop-gives-all-values/24846.md?page=2)
