我有一个用于过滤的自定义模板标记。 get_files_by_type
效果很好,但现在我也想做一个“排除类型”标记。但是,我不知道如何格式化这个,因为排除可能是1个或多个标签。
我如何在函数中执行此操作,如何格式化模板?即模板是吗?
{% exclude_files_by_type Site.sitefiles_set.all 'Site Plan,'Cabinet Photo' as files %}
extras.py
from django import template
from networks.models import SiteFiles
register = template.Library()
@register.assignment_tag
def get_files_by_type(SiteFiles, type):
return SiteFiles.filter(file_type__type=type)
def exclude_files_by_type(SiteFiles, type):
return SiteFiles.exclude
答案 0 :(得分:1)
这样可行:
{% exclude_files_by_type Site.sitefiles_set.all "Site Plan, Cabinet Photo" as files %}
和你的作业标签:
@register.assignment_tag
def exclude_files_by_type(SiteFiles, types):
type_list = [type.strip() for type in types.split(',')]
return SiteFiles.exclude(file_type__type__in=type_list)
要向排除查询添加更多类型,您应将它们放在以逗号分隔的参数中:
{% exclude_files_by_type Site.sitefiles_set.all "Site Plan, Cabinet Photo, Last Summer, Group Selfies" as files %}