Question

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 iterable

I have a model.py:

from django.db import models

# Create your models here.
class BlogPost(models.Model):
    title = models.CharField(max_length=200)
    body = models.TextField()
    create_at = models.DateTimeField()

    def __unicode__(self):
        return self.title

url.py:

from django.conf.urls import patterns, url
from app import views

urlpatterns = patterns('',
    url(r'^$', views.index, name="index"),
    url(r'^(?P<app_id>\d+)/$', views.detail, name='detail'),
    url(r'^(?P<app_id>\d+)/result$', views.result, name="result"),
    url(r'^(?P<app_id>\d+)/vote$', views.vote, name="vote"),
)

and views.py:

from models import BlogPost
from django.http import HttpResponse, Http404
from django.template import RequestContext, loader
from django.shortcuts import render, get_object_or_404, render_to_response

def index(request):
    posts = BlogPost.objects.all()
    context = {'posts': posts}
    return render(request, 'base.html', context)

def detail(request, app_id):
    posts = BlogPost.objects.get(pk=app_id)
    return render_to_response('app/detail.html', {"posts": posts}, context_instance=RequestContext(request))

def result(request, app_id):
    return HttpResponse("To jest strona z wynikami %s" % app_id)

def vote(request, app_id):
    return HttpResponse("Yo tu powinna byc strona z glosowaniem, ale to inny projekt %s" % app_id)

and i get this error:

'BlogPost' object is not iterable

Request Method:     GET
Django Version:     1.6.2
Exception Type:     TypeError
Exception Value:    

'BlogPost' object is not iterable

Exception Location:     /app/.heroku/python/lib/python2.7/site-packages/django/template/defaulttags.py in render, line 155
Python Executable:  /app/.heroku/python/bin/python
Python Version:     2.7.6
Python Path:    

['/app',
 '/app/.heroku/python/bin',
 '/app/.heroku/python/lib/python2.7/site-packages/distribute-0.6.36-py2.7.egg',
 '/app/.heroku/python/lib/python2.7/site-packages/setuptools-2.1-py2.7.egg',
 '/app/.heroku/python/lib/python2.7/site-packages/pip-1.5.4-py2.7.egg',
 '/app',
 '/app/.heroku/python/lib/python27.zip',
 '/app/.heroku/python/lib/python2.7',
 '/app/.heroku/python/lib/python2.7/plat-linux2',
 '/app/.heroku/python/lib/python2.7/lib-tk',
 '/app/.heroku/python/lib/python2.7/lib-old',
 '/app/.heroku/python/lib/python2.7/lib-dynload',
 '/app/.heroku/python/lib/python2.7/site-packages']

Server time:    Mon, 3 Mar 2014 20:32:14 +0000

Here is an error in my template:

Error during template rendering
In template /app/app/templates/base.html, error at line 34

    24  <p><a class="btn btn-lg btn-success" href="#" role="button">Sign up today</a></p>
    25  </div>
    26  {% endblock %}
    27  
    28  {% block content %}{% endblock %}
    29  
    30  
    31  <div class="row marketing">
    32  <div class="container">
    33  {% if posts %}
    34  {% for p in posts %}
    35  <h1><a href="{% url 'detail' p.id %}">{{ p.title }}</a></h1>
    36  <b>{{ p.create_at}}</b>
    37  <p>{{ p.body }}</p>
    38  {% endfor %}
    39  {% else %}
    40  <p>Nie ma zadnych pol do wyswietlenia</p>
    41  {% endif %}
    42  </div>
    43  </div>
    44  

Please for help.

Was it helpful?

Solution

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

views.py

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

template

{% for p in 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><a href="{% url 'detail' posts.id %}">{{ posts.title }}</a></h1>
    <b>{{ posts.create_at}}</b>
    <p>{{ posts.body }}</p>
{% endif %}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top