我有以下简单的模板标记,它返回值列表。
我需要像模板标签一样重写它,所以我可以使用返回列表作为变量

@register.simple_tag
def show_active_filters(cleaned_filter):
  filter_list = []
  if cleaned_filter:
    if cleaned_filter['checkbox']:
        for key, value in cleaned_filter['checkbox'].items():
            for values in value:
                filter_list.append('%s: %s' % (key, values))
    if cleaned_filter['from_to']:
        for filter_name, filter_values in cleaned_filter['from_to'].items():
            sorted_values = sorted(filter_values.values())
            if len(sorted_values) < 2:
                try:
                    filter_values['from']
                    filter_str = u'%s :  от %s' % (filter_name, sorted_values[0])
                    filter_list.append(filter_str)
                except:
                    filter_str = u'%s :  до %s' % (filter_name, sorted_values[0])
                    filter_list.append(filter_str)
            else:
                filter_str = u'%s : от %s -  до %s' % (filter_name, sorted_values[0], sorted_values[1])
                filter_list.append(filter_str)
  return filter_list


我尝试执行此操作的方法如下

@register.tag(name='show_active_filters')
def do_show_active_filters(parser, token):
  error = False
  try:
    tag_name, cleaned_filter, _as, var_name = token.split_contents()
    if _as != 'as':
        error = True
  except:
    error = True

  if error:
    raise template.TemplateSyntaxError, 'invalid stuff"'
  else:
    return CleanedFilterDictionary(cleaned_filter, var_name)

class CleanedFilterDictionary(template.Node):
  def __init__(self, cleaned_filter, var_name='filter_vals'):
    self.cleaned_filter = cleaned_filter
    self.var_name = var_name

def render(self, context):
    context[self.var_name] = get_active_filters(cleaned_filter)
    return ''

def get_active_filters(cleaned_filter):
  filter_list = []
  if cleaned_filter:
    if cleaned_filter['checkbox']:
        for key, value in cleaned_filter['checkbox'].items():
            for values in value:
                filter_list.append('%s: %s' % (key, values))
    if cleaned_filter['from_to']:
        for filter_name, filter_values in cleaned_filter['from_to'].items():
            sorted_values = sorted(filter_values.values())
            if len(sorted_values) < 2:
                try:
                    filter_values['from']
                    filter_str = u'%s :  от %s' % (filter_name, sorted_values[0])
                    filter_list.append(filter_str)
                except:
                    filter_str = u'%s :  до %s' % (filter_name, sorted_values[0])
                    filter_list.append(filter_str)
            else:
                filter_str = u'%s : от %s -  до %s' % (filter_name, sorted_values[0], sorted_values[1])
                filter_list.append(filter_str)
  return filter_list


无法获取如何将cleaned_filter变量传递给类实例

最佳答案

您可以只使用assignment_tag装饰器而不是simple_tag-其余原始代码应按原样工作。

关于python - 需要从simple.tag制作模板标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26590316/

10-12 12:40