Python 如何在django中显示查询集的直方图 目标

Python 如何在django中显示查询集的直方图 目标,python,django,matplotlib,django-views,Python,Django,Matplotlib,Django Views,通常,应用程序显示特定Twitter标签的位置数据。例如,如果用户输入“python”。该应用程序在Twitter上搜索包含标签“python”的最新推文,并在可能的情况下显示这些推文的位置数据 目前,位置数据显示为dict,这是通过Models.py中的def display\u locations(self)功能实现的 现在,我试图在柱状图上显示这些数据。我已经在下面的Views.py中编写了draw\u histogram()。我怀疑一个有效的解决方案可能是调用方法:Hashtag.dis

通常,应用程序显示特定Twitter标签的位置数据。例如,如果用户输入“python”。该应用程序在Twitter上搜索包含标签“python”的最新推文,并在可能的情况下显示这些推文的位置数据

目前,位置数据显示为dict,这是通过Models.py中的
def display\u locations(self)
功能实现的

现在,我试图在柱状图上显示这些数据。我已经在下面的Views.py中编写了
draw\u histogram()。我怀疑一个有效的解决方案可能是调用方法:
Hashtag.display\u locations
在函数中:
draw\u histogram()
,但我不确定

我有两个备选问题:

  • 在视图.py中的
    绘制直方图()
    中,如何设置
    国家/地区列表以实现目标
  • 也许在
    类SearchResultsView(generic.ListView)
    中包含
    draw\u histogram()
    函数作为一种方法更合适?如果是,我该如何做
  • 错误 代码 型号.py

    class Hashtag(models.Model):
        """ Model representing a specific Hashtag serch by user """
        search_text = models.CharField(max_length=140, primary_key=True)
        locations = models.ManyToManyField(Location, blank=True)
    
        def __str__(self):
            """ String for representing the Model object (search_text) """
            return self.search_text
    
        def display_locations(self):
            """ Creates a frequency dict of the locations attached to the Hashtag model """
            country_list = list(self.locations.values_list('country', flat=True).all())
            for country in country_list:
                location_freq = {i:country_list.count(i) for i in set(country_list)}
                return location_freq
    
    class SearchResultsView(generic.ListView):
        """ Generic class-based view listing search results of locations """
        model = Hashtag
        template_name = 'mapping_twitter/results.html'
    
        def get_queryset(self, **kwargs):
            """ Restrict the results to the search_text entered on the Form """
            form_input = self.request.GET.get('search_text')
            qs = Hashtag.objects.all().filter(search_text__iexact=form_input)
            return qs
    
        def get_context_data(self, **kwargs):
            context = super(SearchResultsView, self).get_context_data(**kwargs)
            context['search_text', 'locations'] = self.get_queryset()
            return context
    
    
    def draw_histogram(request, self):
        """ Function to display a histogram of locations associated with the Hashtag """
        country_list = list(self.locations.values_list('country', flat=True).all())
        for country in country_list:
            location_freq = {i:country_list.count(i) for i in set(country_list)}
        plt.bar(list(country_freq.keys()), country_freq.values(), color='g')
        # redirect into the BytesIO object
        f = io.BytesIO()
        plt.savefig(f, format="png", facecolor=(0.95,0.95,0.95))
        plt.clf()
        # Add the contents of the BytesIO object to the response and return
        return HttpResponse(f.getvalue(), content_type="image/png")
    
    视图.py

    class Hashtag(models.Model):
        """ Model representing a specific Hashtag serch by user """
        search_text = models.CharField(max_length=140, primary_key=True)
        locations = models.ManyToManyField(Location, blank=True)
    
        def __str__(self):
            """ String for representing the Model object (search_text) """
            return self.search_text
    
        def display_locations(self):
            """ Creates a frequency dict of the locations attached to the Hashtag model """
            country_list = list(self.locations.values_list('country', flat=True).all())
            for country in country_list:
                location_freq = {i:country_list.count(i) for i in set(country_list)}
                return location_freq
    
    class SearchResultsView(generic.ListView):
        """ Generic class-based view listing search results of locations """
        model = Hashtag
        template_name = 'mapping_twitter/results.html'
    
        def get_queryset(self, **kwargs):
            """ Restrict the results to the search_text entered on the Form """
            form_input = self.request.GET.get('search_text')
            qs = Hashtag.objects.all().filter(search_text__iexact=form_input)
            return qs
    
        def get_context_data(self, **kwargs):
            context = super(SearchResultsView, self).get_context_data(**kwargs)
            context['search_text', 'locations'] = self.get_queryset()
            return context
    
    
    def draw_histogram(request, self):
        """ Function to display a histogram of locations associated with the Hashtag """
        country_list = list(self.locations.values_list('country', flat=True).all())
        for country in country_list:
            location_freq = {i:country_list.count(i) for i in set(country_list)}
        plt.bar(list(country_freq.keys()), country_freq.values(), color='g')
        # redirect into the BytesIO object
        f = io.BytesIO()
        plt.savefig(f, format="png", facecolor=(0.95,0.95,0.95))
        plt.clf()
        # Add the contents of the BytesIO object to the response and return
        return HttpResponse(f.getvalue(), content_type="image/png")
    

    draw_histogram(self,request)@HemanthSP,我按照建议尝试了以下错误:
    TypeError:draw_histogram()缺少一个必需的位置参数:'request'
    draw_histogram()
    的一个实例方法,还是仅仅是一个全局函数?根据您的缩进,它是一个全局函数,但它返回一个
    HttpResponse
    对象。@Tsang YiShen,
    draw\u histogram()
    是一个全局函数。我从这里复制了格式:。如果您知道使用
    draw_histogram()
    作为
    SearchResultsView
    的实例方法来实现目标,那就好了!