views:

295

answers:

1

I'm trying to customise a CMS written in Django. The content editors aren't flexible enough so I'm trying to come up with a better solution.

Without over-explaining it, I'd like it to be a bit like django-better-chunks or django-flatblocks. You set up an editable region entirely from within the template. I want to bind these editable regions to a mix of strings and object instances. One example would be having multiple editable regions based on one product:

{% block product_instance "title" %}
{% block product_instance "product description" %}

So if you have a view with another product as product_instance those two blocks would show different data. I would also see there being use for site-wide blocks that only pass through the string part. Essentially, I would like to be able to pass 1-infinity identifiers.

But I'm really struggling on two fronts here:

  1. How do I define the relationship between the mixed identifier and the actual content "block" instance? I have a feeling contenttypes might feature here but I've really no idea where to start looking!

  2. And how do I write a template tag to read the above syntax and convert that into an object for rendering?

+2  A: 

for this you can create an inclusion tag and use it like:

{% load my_tags %}
{% product bicycle <extra vars ...> %}

To define the tag, add to your app/templatetags/mytags.py:

@register.inclusion_tag('results.html')
def product(item, *extra):
    #maybe repackage extra variables
    #and add them to the returned dictionary
    item_form = ItemForm(item) #form.ModelForm instance
    return {'item': item, 'item_form':item_form, ...}

Then you'll need a template that returns html for the item:

<h1>{{item.title}}</h1>
{{item_form}}
... add some conditional statements depending on extra vars
Evgeny