I'm creating a reddit clone as a personal project to get to grips with django, and so far I've got two classes:
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
And in my urls I have:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^(?P<board_name>\w+)/$', views.board_lv)
)
What I want to do is have the url pattern compare against the "name" field in the "Board" model using the "board" foreign key in the "Notice" model, and then open up a page listing all the notices with in that board (similar to subreddits in reddit).
I'm getting a type error saying "Notice object is not iterable", which leads me to think I've only got one object instead of a list, but I shouldn't have should I?
The iteration is in this part of the noticeList.html file;
{% 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 %}
where the views are:
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)
Also, in the error traceback, the board_name variable value is preceded by a 'u' before the quoted string, e.g:
Variable Value
board_name u'worldnews'
If I'm getting the url pattern wrong or haven't explained the problem well enough, please let me know as any help would be greatly appreciated.
EDIT: full traceback error
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