[Answered ]-Writing a Template Tag in Django

2👍

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

Leave a comment