Return template as string - Django

Posted by Ninefingers on Stack Overflow See other posts from Stack Overflow or by Ninefingers
Published on 2010-03-29T21:43:05Z Indexed on 2010/03/29 21:53 UTC
Read the original article Hit count: 1058

Hi All,

I'm still not sure this is the correct way to go about this, maybe not, but I'll ask anyway. I'd like to re-write wordpress (justification: because I can) albeit more simply myself in Django and I'm looking to be able to configure elements in different ways on the page. So for example I might have:

  • Blog models
  • A site update message model
  • A latest comments model.

Now, for each page on the site I want the user to be able to choose the order of and any items that go on it. In my thought process, this would work something like:

class Page(models.Model)
    Slug = models.CharField(max_length=100)

class PageItem(models.Model)
    Page = models.ForeignKey(Page)
    ItemType = models.CharField(max_length=100)
    InstanceNum = models.IntegerField() # all models have primary keys.

Then, ideally, my template would loop through all the PageItems in a page which is easy enough to do.

But what if my page item is a site update as opposed to a blog post? Basically, I am thinking I'd like to pull different item types back in different orders and display them using the appropriate templates. Now, I thought one way to do this would be to, in views.py, to loop through all of the objects and call the appropriate view function, return a bit of html as a string and then pipe that into the resultant template.

My question is - is this the best way to go about doing things? If so, how do I do it? If not, which way should I be going? I'm pretty new to Django so I'm still learning what it can and can't do, so please bear with me. I've checked SO for dupes and don't think this has been asked before...

© Stack Overflow or respective owner

Related posts about python

Related posts about django