django: recursive/tree-like URL mapping for a CMS?
I am rebuilding a website that features a lot of content contributed by the users (posts,pictures,events etc). This content falls under sections (music, shop etc.); i.e. a normal CMS.
I want to have each piece of content inherit from a base content object (multi table inheritence). This means all content can be queried easily and is neat and extensible (and no need to re-write all the basic stuff like 'created_by', 'title', 'slug' etc.)
ContentObj
> Blog Post obj
> Event Post obj
> Podcast Post obj
> ... etc.
Pretty straightfoward.
When it comes to organisation, the various content objs/models should have the option of being assigned to a section (i.e. podcast ob开发者_运维知识库ject comes under 'Music'). Alternatively, a section should be able to assign itself to a parent section ('Words > Blog > General > '). Again this is a pretty bog-standard idea in CMSs.
Ideally,I want a completely generic solution to organisation like this (just as I have a generic solution to content). I am thinking of a tree-like structure, where each node is either a section, or a contentobject model.
root
> section1
> subsection 1 > contentobj1
> subsection 2 > contentobj2
> section2
> contentobj3
> contentobj4
The goal with this generic setup, is to have a very DRY-esqe URL scheme and template system. You would only need two or three templates. You populate the page depending on what url the user has requested and never need to hard-code urls such as /blog///.
I'm having trouble understanding how to write this tree like structure in a database driven app. It would be easy to have a model Section to which you can assign content models. But when it comes to subsections it's a little trickier. Furthermore how to abstract the URL scheme so that a section can have as many subsections that point to a content object without actually hardcoding the urls?
Basically I want to write a structure/skeleton, where content and organisation are separate, and the url/template scheme and completely separate to the the actual content. Previously I would have had a blog-app, an event-app but surely I should have a content-app that manages all the content of the site, and maybe an organisation-app that manages the sections etc.
For implementing a tree in your models, checkout the trees and graphs section on django packages. i have used and would recommend django-mptt
On a side note, why dont you have a look at django-cms. Its a great application thats really flexible and has a lot of extension points for integrating your own applications.
I like this question. It's something I've had percolating in the back of my head since leaving plone-land (not that I don't still go back there from time to time).
I'd definitely start with some tree app (like zsquare suggested) and build a base model that makes use of a Generic Foreign Key to link in the various content types you talk about - Events, Articles, Categories, etc.
From there I'd dig into the admin to see how it deals with the dynamic url generation and go from there, making use of the functions the tree package provides to build up the urls for my content tree.
精彩评论