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>