views:

190

answers:

2

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) # tells me which model to display
    InstanceNum = models.IntegerField() # tells me which instance of which model...

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...

I've also looked at Django-cms to see if that helps, but I couldn't get to grips with it.

Any suggestions?

+1  A: 

The include template tag can take a variable containing the template to include, so you could loop through a sequence containing the various sub-templates and include them in turn, maybe using a dict to map friendly names to template filenames.

Ignacio Vazquez-Abrams
...and a sort field so the user can specify the order. +1 Thanks, I'll look into it.
Ninefingers
+2  A: 

First, some puzzelement.

 InstanceNum = models.IntegerField() # all models have primary keys.

In Django, all model are assigned an integer primary key.

The comment doesn't make sense, since you don't need to add a primary key like this. The PageItem already has a primary key.

Also, please use lower case letters for attributes. Only Use Upper Case for Class Names. Please.

"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"

Different types usually means different models. Rather than a vague "PageItem", you probably want to have "Site Update" and "Blog Post" as separate models.

You can then iterate through these various objects and display them in the template.

You can easily have your various Models defined with a method to return HTML information. You don't (generally) want to return fully-baked HTML. But CSS ID or Class information is sometimes helpful.

class SiteUpdate( models.Model ):
    page = models.ForeignKey(Page)
    item_text = models.CharField(max_length=100)
    item_css_class = models.CharField(max_length=64)

Now you can generate this into the template with a simple <div class="{{item.item_css_class}}">{{item.item_text}}</div> and use CSS to handle the formatting details that distinguish site update as opposed to a blog post.

S.Lott
InstanceNum wasn't to refer to the Instance of PageItem, it was to refer to an instance of any other modelled item which is why I want to be able to pass each model through a template in the first place - they aren't all just text fields. What if, for example, I want a model containing three fields to be displayed on the top of each page? And then if I want to change its position later...? I've looked at Django-cms to see if I would be better using that... I can't find any tutorials on it though.
Ninefingers
@Ninefingers: (1) Please UPDATE your question with the additional facts. No one will look down here to find this important information. (2) "PageItem" is still uselessly vague for all of this additional stuff you're talking about. Your model -- and your templates -- can't be uselessly vague and still do anything.
S.Lott