我的模型和模板有问题,我确信模型处于最佳状态。我点击主页面上的链接,我收到一个错误:'BlogPost'对象不可迭代
我有一个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"),
)
和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)
我收到此错误:
'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
我的模板中出现错误:
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
请求帮助。
答案 0 :(得分:2)
您无法迭代get()
查询。您有以下内容:
views.py
posts = BlogPost.objects.get(pk=app_id)
模板
{% for p in posts %} {% comment %} won't work {% endcomment %}
filter()
总是返回一个QuerySet(可迭代),而Manager上的get()
方法总是直接返回一个对象(因此不可迭代)。
您可以在模板中尝试此操作:
{% 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 %}