Reputation: 3777
I'm trying to create my own templatetag. How I've done this:
folder structure:
my_app/
__init__.py
models.py
views.py
my_app/
templates/
show.html
templatetags/
__init__.py
depos.py
depos.py:
# coding: utf-8
from django import template
from core.models import Depos
register = template.Library()
@register.inclusion_tag('show.html')
def show_dep():
dep = Depos.objects.all().order_by('?')[0]
return dep
show.html:
<div id="user_testimonial">
<blockquote>
<p>{{ dep.dep }}</p>
<cite>{{ dep.name }}, {{ dep.from }}</cite>
</blockquote>
</div>
in my templates:
{% load depos %}
{% show_dep %}
but I've got this error:
TypeError at /cadastro
'Depos' object does not support item assignment
Upvotes: 3
Views: 1298
Reputation: 53971
You need to be passing a dictionary object from your inclusion tag to your inclusion tag template. It's mentioned in the docs:
First, define the function that takes the argument and produces a dictionary of data for the result. The important point here is we only need to return a dictionary, not anything more complex.
so try:
@register.inclusion_tag('show.html')
def show_dep():
return {
'dep' : Depos.objects.all().order_by('?')[0]
}
Upvotes: 7