Sunday, May 1, 2011

How can you have Wordpress-like custom fields using Django flatpages?

One of the features that makes a CMS like WordPress powerful is the ability to add additional fields that may be used in the template. WordPress has what it calls custom fields. Is there a way to do that using Django's flat pages? If not, is there another Django app that will allow page creation with the option of adding additional fields?

From stackoverflow
  • You could roll your own version of the flatpages app; take a look at the source code, it's really simple.

    It sounds like you could accomplish what you want with two models: one to represent the Pages, and another to represent CustomFields. Tie them together with foreign keys and some inline admin goodness, and you should be set. To access the fields in your template, make sure the Page class implements some kind of lookup function, like __getitem__.

  • If you don't need to query on the fields then the easiest way is to add a field that holds all the custom data either in a pickled dictionary or some other format.

    JSONField is an example of storing custom objects in JSON format.

  • Flatpages are basically just static HTML pages that are stored in your database with a pretty admin interface.

    If you need something more complex, it is time to roll your own.

0 comments:

Post a Comment