Django 1.1 template question

Posted by Bovril on Stack Overflow See other posts from Stack Overflow or by Bovril
Published on 2010-06-09T13:32:45Z Indexed on 2010/06/09 13:42 UTC
Read the original article Hit count: 255

Filed under:
|
|

Hi All,

I'm a little stuck trying to get my head around a django template.

I have 2 objects, a cluster and a node

I would like a simple page that lists...

[Cluster 1]
[associated node 1]
[associated node 2]
[associated node 3]

[Cluster 2]
[associated node 4]
[associated node 5]
[associated node 6]

I've been using Django for about 2 days so if i've missed the point, please be gentle :)

Models -

class Node(models.Model):
    name = models.CharField(max_length=30)
    description = models.TextField()
    cluster = models.ForeignKey(Cluster)

    def __unicode__(self):
        return self.name

class Cluster(models.Model):
    name = models.CharField(max_length=30)
    description = models.TextField()

    def __unicode__(self):
        return self.name

Views -

def DSAList(request):

    clusterlist = Cluster.objects.all()
    nodelist = Node.objects.all()

    t = loader.get_template('dsalist.html')
    v = Context({
                 'CLUSTERLIST' : clusterlist,
                 'NODELIST' : nodelist,
               })

    return HttpResponse(t.render(v))

Template -

<body>
    <TABLE>
    {% for cluster in CLUSTERLIST %}
        <tr>
         <TD>{{ cluster.name }}</TD>
                 {% for node in NODELIST %}
                     {% if node.cluster.id == cluster.id %}
                     <tr>
                       <TD>{{ node.name }}</TD>
                     </tr>
                     {% endif %}
                 {% endfor %}
        </tr>
    {% endfor %}
    </TABLE>
</body>

Any ideas ?

© Stack Overflow or respective owner

Related posts about django

Related posts about django-templates