1

私はdjangoを理解するための個人的なプロジェクトとしてredditクローンを作成しています。これまでのところ、2つのクラスがあります:

from django.db import models
from django.contrib.auth.models import User

class Board(models.Model):
    name = models.CharField(max_length =100, blank = False)
    created_on = models.DateTimeField('Date Created', auto_now_add=True)
    deleted = models.BooleanField(default = False)

    def __unicode__(self):
        return self.name

class Notice(models.Model):
    title = models.CharField(max_length=200) #title as it appears on the notice
    author = models.ForeignKey(User, null=False, blank=False)
    posted_on = models.DateTimeField('Posted On', auto_now_add= True)
    updated_on = models.DateTimeField('Last Updated', auto_now = True)
    board = models.ForeignKey(Board, null=False)
    isText = models.BooleanField(null=False, blank = False)
    content = models.TextField()
    thumps_up = models.PositiveIntegerField()
    thumps_down = models.PositiveIntegerField()
    deleted = models.BooleanField(default=False)

    def __unicode__(self):
        return self.title

そして、私のURLには次のものがあります:

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^(?P<board_name>\w+)/$', views.board_lv)
)

私がやりたいことは、「通知」モデルの「ボード」外部キーを使用して「ボード」モデルの「名前」フィールドと URL パターンを比較し、その中にあるすべての通知をリストするページを開くことです。ボード (reddit の subreddits に似ています)。

「Notice object is not iterable」という型エラーが発生しました。これにより、リストではなく 1 つのオブジェクトしか取得できなかったと思われますが、そうすべきではないのでしょうか?

繰り返しは、noticeList.html ファイルのこの部分にあります。

{% for n in latest_notices %}
    <li>
    {% if not n.isText %} 
        <h2><a href="{{ n.content }}">{{ n.title }}</a></h2>            
    {% else %}
        <h2>{{ n.title }}</h2>
        <p>{{ n.content }}</p>
    {% endif %}
    </li>
{% endfor %}

ビューは次のとおりです。

from django.shortcuts import render, get_object_or_404
from notices.models import Board, Notice

def index(request):
    latest_notices = Notice.objects.order_by('-posted_on')
    context = {'latest_notices': latest_notices}
    return render(request, 'noticeList.html', context)

def board_lv(request, board_name):
    latest_notices = get_object_or_404(Notice, board__name=board_name)
    context = {'latest_notices': latest_notices}
    return render(request, 'noticeList.html', context)

また、エラー トレースバックでは、board_name 変数値の前に、引用符で囲まれた文字列の前に「u」が付きます。次に例を示します。

Variable        Value
board_name      u'worldnews'

URL パターンが間違っている場合、または問題を十分に説明していない場合は、お知らせください。

編集: 完全なトレースバック エラー

Traceback:
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.2-py2.7.egg/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/adnan/Documents/django-noticeboard/noticeboard/notices/views.py" in board_lv
  13.   return render(request, 'noticeList.html', context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.2-py2.7.egg/django/shortcuts/__init__.py" in render
  53.     return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.2-py2.7.egg/django/template/loader.py" in render_to_string
  177.         return t.render(context_instance)
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.2-py2.7.egg/django/template/base.py" in render
  140.             return self._render(context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.2-py2.7.egg/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.2-py2.7.egg/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.2-py2.7.egg/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.2-py2.7.egg/django/template/defaulttags.py" in render
  284.                 return nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.2-py2.7.egg/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.2-py2.7.egg/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.2-py2.7.egg/django/template/defaulttags.py" in render
  147.             values = list(values)

Exception Type: TypeError at /b/worldnews/
Exception Value: 'Notice' object is not iterable
4

1 に答える 1

1

問題は、 1 つのモデル インスタンスをget_object_or_404(Notice, board__name=board_name)返すことであり、反復可能ではありません。Notice

考えられる解決策の 1 つは、これをリストに追加してコンテキストに渡すことです。

def board_lv(request, board_name):
    latest_notices = get_object_or_404(Notice, board__name=board_name)
    context = {'latest_notices': [latest_notices]}
    return render(request, 'noticeList.html', context)

次のエラーの UPD:ボードごとget_object_or_404に複数の が存在する可能性がある場合は使用しないでください。Notice使用filter:

def board_lv(request, board_name):
    latest_notices = Notice.objects.filter(board__name=board_name)
    context = {'latest_notices': latest_notices}
    return render(request, 'noticeList.html', context)

それが役立つことを願っています。

于 2013-08-23T20:46:32.117 に答える