djangoのtemplate filter別種使用
通常、filterを使用すると、時間フォーマットの調整、文字列の切断など、元のデータの直接処理が行われます.私は小さなプロジェクトの中で1つのディレクトリの下のサブアイテムに基づいて、3つごとに1行として表示されます.djangoのtemplateは論理を書くことができないので、filterを使ってidを転送し、生成されたhtmlコードを返します.
生存効果:
templateコード
フィルタコード
生存効果:
××××××××××××××××××××××××××××
==== ==== ===
templateコード
{% load insititute %} load the filter
{% for obj in object_list %}
<table id="zone_title_bar">
<tr><td id='zone_icon'></td><td>{{ obj.name }}</td></tr>
</table>
{{ obj.id|insititute_list|safe }} id
{% endfor %}
フィルタコード
#coding=utf-8
from django import template
from django.template import Library
from bnu.apps.teacher.models import Entry,Institute
register = Library()
def insititute_list(id):
"Removes all values of arg from the given string"
objects = Entry.objects.get(id__exact=int(id)).institute_set.all()
count = 0
str ="<table>"
for obj in objects:
if(count % 3 == 0):
str += "<tr>"
strid= "%s" % obj.id
str += "<td class='insititute_item'><a href='/insititute/"+strid+"/' target='_blank'>"+obj.name+"</td>"
count += 1
if(count %3 == 0):
str += "</tr>"
if(count % 3) != 0:
str += "</tr>"
str +="</table>"
return str
# , 4 , td
register.filter('insititute_list', insititute_list)