Skip to content Skip to sidebar Skip to footer

Writing A Template Tag In Django

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

Solution 1:

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')defproduct(item, *extra):
    #maybe repackage extra variables#and add them to the returned dictionary
    item_form = ItemForm(item) #form.ModelForm instancereturn {'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

Post a Comment for "Writing A Template Tag In Django"