Yeah sure.

Models.py:

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

class Category(models.Model):
    name = models.CharField(max_length=100)

    class Meta:
        verbose_name_plural = 'categories'

    def __unicode__(self):
        return self.name

class Forum(models.Model):
    category = models.ForeignKey(Category,related_name='forums')
    name = models.CharField(max_length=100)

    def __unicode__(self):
        return self.name

class Topic(models.Model):
    forum = models.ForeignKey(Forum,related_name='topics')
    subject = models.CharField(max_length=100)
    views = models.IntegerField()

    def __unicode__(self):
        return self.subject()

class Post(models.Model):
    topic = models.ForeignKey(Topic,related_name='posts')
    created = models.DateTimeField(auto_now_add=True)
    body = models.TextField()
    author = models.ForeignKey(User,related_name='posts')

    def __unicode__(self):
        return self.subject

Views.py:

from django.shortcuts import render_to_response
import models

def index(request):
    categories = models.Category.objects.all()
    return render_to_response("forum/index.html",
{'categories':categories})

Index.html:

{% extends "base.html" %}

{% block title %}EvoWebs{% endblock %}

{% block body %}
{% if categories %}
{% for category in categories %}
<table border="1">
<tr><td>{{category.name}}</td></tr>
{% for forum in category.forums %}
<tr><td>{{forum.name}}</td></tr>
{% endfor %}
</table>
{% endfor %}
{% else %}
<p>This forum has no categories</p>
{% endif %}
{% endblock %}

Base.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en-GB" xml:lang="en-GB" xmlns="http://www.w3.org/1999/
xhtml">
<head>
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>

On Aug 4, 7:29 pm, Alex Gaynor <alex.gay...@gmail.com> wrote:
> On Tue, Aug 4, 2009 at 1:16 PM,
>
>
>
> british.assassin<british.assas...@gmail.com> wrote:
>
> > Hi,
>
> > In the views.py for my app I have the following:
>
> > def index(request):
> >    categories = models.Category.objects.all()
> >    return render_to_response("forum/index.html",
> > {'categories':categories})
>
> > And in the template file I am trying to iterate through these via
> > doing:
>
> > {% for category in categories %}
>
> > But every time I try this I get an exception saying: 'RelatedManager'
> > object is not iterable.
>
> > I was under the impression that the output from obkects.all() was
> > iterable?
>
> > I would appreciate any help anyone can give.
>
> > Thanks
>
> It is, you've done something, either in your template or in your
> models that's somehow causing the issue.  If you could show us those
> we'd have a better idea of what the issue is.
>
> Alex
>
> --
> "I disapprove of what you say, but I will defend to the death your
> right to say it." -- Voltaire
> "The people's good is the highest law." -- Cicero
> "Code can always be simpler than you think, but never as simple as you
> want" -- Me
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to