Skip to content Skip to sidebar Skip to footer

My Object Is Not Iterable

I have a problem with the model and template, and I am sure that the model is in the best order. I click on the link on the main page and I get an error: 'BlogPost' object is not i

Solution 1:

You cannot iterate over a get() query. You have the following:

views.py

posts = BlogPost.objects.get(pk=app_id)

template

{% forpin posts %}  {% comment %} won't work {% endcomment %}

filter() always returns a QuerySet (which is iterable), whereas the get() method on a Manager always returns a single object directly (and thus is not iterable).

You could try this in your template instead:

{% if posts %} {% comment %} no for clause {% endcomment %}
    <h1><ahref="{% url 'detail' posts.id %}">{{ posts.title }}</a></h1><b>{{ posts.create_at}}</b><p>{{ posts.body }}</p>
{% endif %}

Post a Comment for "My Object Is Not Iterable"