templatetag:QuerySet'对象没有属性'getlist'

时间:2017-11-17 15:26:16

标签: django django-templates

在尝试按照修改的模板标签的书籍示例时,我遇到以下错误:

QuerySet' object has no attribute 'getlist'report_id=88&report_id=89 HTTP/1.1" 500 180158

我按照向我的app目录添加templatetags的说明进行了操作,并包含了 init .py和app_filter.py,我想过滤一下,如下:

Security

        accounts

           templatetags

                app_filters.py

                __init__.py

我的app_filter.py定义如下:

from django import template
register = template.Library()

@register.filter
def get_list(querydict, itemToGet ):

    return querydict.getlist(itemToGet)

我的settings.py包含以下内容:

INSTALLED_APPS = [
    'django_python3_ldap',
    'django_extensions',
    'django_filters',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'accounts',
]

我的观点是正确传递数组:

checked = request.GET.get('report_id')
checkedlist = request.GET.getlist('report_id')


reportlist = QvReportList.objects.filter(report_id__in= checkedlist, active = 1).values_list('report_name_sc',flat = True)


print (checked)
print (checkedlist)


args = {'retreivecheckbox': reportlist}

return render(request,'accounts/requestaccess.html', args)

在执行打印清单时,我在控制台中看到了数组:

['75', '76', '77']

我的模板如下:

{% load app_filters %}
{% for reports in retreivecheckbox %}

        {{ retreivecheckbox|get_list:'report_id' }}
    </div>
{% endfor %}

2 个答案:

答案 0 :(得分:0)

您正在使用路径调用@register.filter。我认为你的意思是用@register.filter('get_list')

来称呼它

Take a look at the docs:这是两个用例:

@register.filter(name='cut')
def cut(value, arg):
    return value.replace(arg, '')

@register.filter
def lower(value):
    return value.lower()

这些会创建两个过滤器,lowercut

或者,您可以调用@register.filter并将名称作为第一个参数传递,如下所示:

register.filter('cut', cut)
register.filter('lower', lower)

答案 1 :(得分:0)

我不得不使用{{retreivecheckbox | get_list:report_id}}

而不是{{retreivecheckbox | get_list:'report_id'}}

{% load app_filters %}
{% for reports in retreivecheckbox %}

        {{ retreivecheckbox|get_list:report_name_sc }}

    </div>
{% endfor %}