我正在尝试创建自己的模板标签。我是如何做到的:
文件夹结构:
my_app/
__init__.py
models.py
views.py
my_app/
templates/
show.html
templatetags/
__init__.py
depos.py
仓库.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
显示.html:
<div id="user_testimonial">
<blockquote>
<p>{{ dep.dep }}</p>
<cite>{{ dep.name }}, {{ dep.from }}</cite>
</blockquote>
</div>
在我的模板中:
{% load depos %}
{% show_dep %}
但我有这个错误:
TypeError at /cadastro
'Depos' object does not support item assignment