How do I create list and detail views for django-taggit?

柔情痞子 提交于 2019-12-03 01:36:50

2. I believe this is for returning a single object, not multiple objects.

def get_object(self):  

Instead perhaps you should try something like the following:

def get_queryset(self):
    return TaggedItem.objects.filter(tag__iexact=self.kwargs['tag'])

This returns a list of items with GenericForeignKeys

If you are only interested in a specific model called App then

    return App.objects.filter(tags__name__in=[self.kwargs['tag']])

Default variable name in the template is TaggedItem_list then

{% for item in TaggedItem_list %}
   {{item.content_object}} {# generic foreign key here #}
{% endfor %}

The urls.py would have to be similar to

url(r'someapp/(?P<tag>\w+)/$', TaggedList.as_view())
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!