Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django Taggit:如何打印最常用的标签?_Python_Django - Fatal编程技术网

Python Django Taggit:如何打印最常用的标签?

Python Django Taggit:如何打印最常用的标签?,python,django,Python,Django,事实上,我正在使用django应用程序。我想知道你是否知道打印最常见标签的方法。我试过了,但没用。。。(给你)。有人能帮帮我吗?我的解决方案可能有问题,但很有效。尝试: from collections import defaultdict, Counter from taggit.models import Tag from .models import YourModel tag_frequency = defaultdict(int) for item in YourModel.ob

事实上,我正在使用django应用程序。我想知道你是否知道打印最常见标签的方法。我试过了,但没用。。。(给你)。有人能帮帮我吗?

我的解决方案可能有问题,但很有效。尝试:

from collections import defaultdict, Counter
from taggit.models import Tag
from .models import YourModel

tag_frequency = defaultdict(int)


for item in YourModel.objects.all():
    for tag in item.tags.all():
        tag_frequency[tag.name] += 1

Counter(tag_frequency).most_common()
因此,在基于类的视图中,这可能看起来像:

from collections import defaultdict, Counter
from taggit.models import Tag
from .models import YourModel


class YourView(ListView):
    model = YourModel
    context_object_name = "choose_a_name_you_like"
    template_name = "yourmodel/yourmodel_list.html"

    def get_context_data(self):
        context = super(YourView, self).get_context_data() # assigns the original context to a dictionary call context
        tag_frequency = defaultdict(int)

        for item in YourModel.objects.all():
            for tag in item.tags.all():
                tag_frequency[tag.name] += 1

        context['tag_frequency'] = Counter(tag_frequency).most_common() # adds a new item with the key 'tag_frequency' to the context dictionary, which you can then access in your template
        return context

我的解决方案可能有点老套,但很有效。尝试:

from collections import defaultdict, Counter
from taggit.models import Tag
from .models import YourModel

tag_frequency = defaultdict(int)


for item in YourModel.objects.all():
    for tag in item.tags.all():
        tag_frequency[tag.name] += 1

Counter(tag_frequency).most_common()
因此,在基于类的视图中,这可能看起来像:

from collections import defaultdict, Counter
from taggit.models import Tag
from .models import YourModel


class YourView(ListView):
    model = YourModel
    context_object_name = "choose_a_name_you_like"
    template_name = "yourmodel/yourmodel_list.html"

    def get_context_data(self):
        context = super(YourView, self).get_context_data() # assigns the original context to a dictionary call context
        tag_frequency = defaultdict(int)

        for item in YourModel.objects.all():
            for tag in item.tags.all():
                tag_frequency[tag.name] += 1

        context['tag_frequency'] = Counter(tag_frequency).most_common() # adds a new item with the key 'tag_frequency' to the context dictionary, which you can then access in your template
        return context

Django 1.10、Python 3.5、Django taggit 0.21.3

YourModel.tags.most_common()
和前10个标签:

YourModel.tags.most_common()[:10]

Django 1.10、Python 3.5、Django taggit 0.21.3

YourModel.tags.most_common()
和前10个标签:

YourModel.tags.most_common()[:10]

非常感谢,但是这段代码是在哪里编写的呢?视图。py?很抱歉离开了一段时间。是的,您可以将这是一个视图,在这种情况下,您将返回您的计数作为一些额外的上下文。我将用一个基于类的示例视图编辑答案。非常感谢,但是这段代码是在哪里编写的呢?视图。py?很抱歉离开了一段时间。是的,您可以将这是一个视图,在这种情况下,您将返回您的计数作为一些额外的上下文。我将使用基于类的示例视图编辑答案。