Django从HTML模板中的视图遍历列表

时间:2018-10-04 09:46:21

标签: django django-templates django-views

我问我如何在基于django类的视图中获取for loop到HTML模板。

用户可以选中一个或多个复选框,我想显示每个选中复选框的信息。

我认为:

checkbox_list = self.request.GET.getlist('PUBSDChoice') #Get object id checked by user
context_data['checkbox_list'] = checkbox_list

for checkbox in checkbox_list:
    pubsd = Publication.objects.get(id=checkbox)  
    get_request = Download.objects.filter(pub__publication__id=pubsd.id).count()

    context_data['pubsd'] = pubsd
    context_data['get_request'] = get_request

现在,在我的模板中,我有:

<table>
    <tbody>
    <tr>
        <th>{% trans 'Publication ID' %}</th>
        <th>{% trans 'Requests' %}</th>
    </tr>
    {% for item in checkbox_list %} # I'm not sure for this loop
    <tr>
        <td>{{ pubsd.pub_id }}</td> #Do I have to add item.??
        <td>{{ get_request }}</td>
    </tr>
    {% endfor %}
    </tbody>
</table>

非常感谢您

编辑:

这是我的视图内所有查询集的真实代码:

checkbox_list = self.request.GET.getlist('PUBSDChoice')
context_data['checkbox_list'] = checkbox_list

for checkbox in checkbox_list:
    pubsd = Publication.objects.get(id=checkbox)  # display publication id

    # Count number of requests in download table for checked publication
    get_request = Download.objects.filter(pub__publication__id=pubsd.id).count()
    #Get sum of publication usage (= downloads) for checked publication
    get_download = Download.objects.filter(pub__publication__id=pubsd.id).aggregate(get_sum_usage=Sum('usage'))

    # Get country which maximum download the checked publication
    get_country = Country.objects.filter(download__pub__publication__id=pubsd.id).annotate(
                ndown=Count('download')).order_by('-ndown').first()
    # Get customer which maximum download the checked publication
    get_customer = Download.objects.filter(pub__publication__id=pubsd.id).values('email').order_by(
                'email').annotate(count_email=Count('email')).first()
    # Get the best downloaded format for the checked publication
    get_format = Download.objects.filter(pub__publication__id=pubsd.id).values('pub__format').annotate(
                format_count=Count('pub__format')).order_by('-format_count').first()

    context_data['pubsd'] = pubsd
    context_data['get_request'] = get_request
    context_data['get_download'] = get_download
    context_data['get_country'] = get_country
    context_data['get_customer'] = get_customer['email']
    context_data['get_format'] = get_format['pub__format']

这正是我的模型:

class Publication(models.Model):
    pub_id = models.CharField(max_length=10, verbose_name=_('publication ID'), default='')
    title = models.CharField(max_length=512, verbose_name=_('publication title'), null=False, unique=True)
    category = models.ForeignKey(Category, verbose_name=_('category'), null=False, related_name='publication')
    nb_document = models.IntegerField(verbose_name=_('number of documents'), default=0)
    nb_download = models.IntegerField(verbose_name=_('number of downloads'), default=0)
    new_publication = models.BooleanField(verbose_name=_('new publication'), default=True)

class Document(models.Model):
    language = models.CharField(max_length=2, verbose_name=_('language'), choices=LANGUAGE_CHOICES, null=False)
    format = models.CharField(max_length=10, verbose_name=_('format'), choices=FORMAT_CHOICES, null=False)
    title = models.CharField(max_length=512, verbose_name=_('document title'), null=False)
    publication = models.ForeignKey(Publication, verbose_name=_('publication title'), null=False,
                                    related_name='documents')
    nb_download = models.IntegerField(verbose_name=_('number of downloads'), default=0)

class Download(models.Model):
    email = models.CharField(max_length=150, verbose_name=_('e-mail'), null=False)
    country = models.ForeignKey(Country, verbose_name=_('country'), related_name='download')
    pub = models.ForeignKey(Document, verbose_name=_('Publication ID'), null=False)
    usage = models.IntegerField(verbose_name=_('usage'), default=0)

class Customer(models.Model):
    email = models.CharField(max_length=150, verbose_name=_('e-mail'), null=False)
    country = models.ForeignKey(Country, verbose_name=_('country'))
    institution = models.CharField(max_length=255, verbose_name=_('institution'), null=True)

正如我之前所说,对于一个已检查的出版物,它可以正常工作,但是当我要检查多个出版物时,它是不工作的。

如果有人可以帮助我进行转换,那就太好了!否则,我将尝试使用以前做的好答案:)

1 个答案:

答案 0 :(得分:2)

这里每次都覆盖键的先前值。结果,最后的context_data仅包含在 last 迭代中提取的值。

checkbox_list = self.request.GET.getlist('PUBSDChoice')
context_data['checkbox_list'] = checkbox_list

context_data['pubsd'] = Publication.objects.filter(
    id__in=checkbox_list
).annotate(
    ndown=Count('publication__download')  # or something related
)

因此,我们对查询集进行过滤,使其仅包含列表中带有Publication的{​​{1}},并为下载次数添加注释(表达式可能有些不同,因为您没有包括id)。

以上内容将根据models.py进行排序。如果需要,您可以对其进行后排序。

然后,您可以在模板中使用以下命令呈现该图片:

checkbox_list

因此,我们遍历{% for item in pubsd %} <tr> <td>{{ item.pub_id }}</td> <td>{{ item.ndown }}</td> </tr> {% endfor %} 查询集,并为每个pubsd(这是一个item对象)渲染Publicationitem.pub_id