Return template as string - Django

By : s mohan
Source: Stackoverflow.com
Question!

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?

By : s mohan


Answers

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.



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

By : S.Lott


This video can help you solving your question :)
By: admin