[Django]-Django 'User' object is not iterable

7πŸ‘

βœ…

You have a mixup in your logic, your detail refers to followers but the field itself is a link to a singular user, you either need to make this field a ManyToMany relationship, or use a reverse lookup to find what a user follows.

(Theres also a stray comma in your context dict which may cause issues later on..

πŸ‘€Sayse

2πŸ‘

get returns a single queryset and you cannot iterate over it, if you use get

use this in template

   <h1>{{ detail.followers }}</h1>

or if you need multiple

in view

twi = Follow.objects.filter(pk=current_user.id)

and change this line

display = twi.followers

to

display = twi

and in template

{% for o in detail %}
   <h1>{{ o.followers }}</h1>
{% endfor %}
πŸ‘€Exprator

0πŸ‘

if you want to iterate over β€˜detail’ and do not want to change your code in the templates, use a filter() query and set 'display' to result of your filter query.

or if you are not planning to change your views code you can edit the template code from

{% for o in detail %}
<h1>o.followers</h1>
{% endfor %}

to just

<h1>{{detail}}</h1>

πŸ‘€badiya

Leave a comment