在Django的模板中重现

时间:2017-04-11 21:28:10

标签: python django templates

In [23]: perc = Perception.objects.all()    

In [24]: list = []

In [25]: for item in perc:
    ...:     if item.loan.request.customer_id not in list:
    ...:         print(item.loan.request.customer.user.last_name, item.loan.requ
    ...: est.customer.user.first_name)
    ...:     list.append(item.loan.request.customer_id)
    ...:     
(u'Kshlerin', u'Dwight')
(u'Boyer', u'Keshaun')

我想在模板中重现这样的循环。

以下是与该模板相关的视图:

class PerceptionIndexView(StaffRestrictedMixin, FrontendListView):
    page_title = _('Perception')
    model = Perception
    template_name = 'loanwolf/perception/index.html'
    pjax_template_name = 'loanwolf/perception/index.pjax.html'
   #row_actions_template_name = 'loanwolf/perception/list-actions.inc.html'
    url_namespace = 'perception'

   #def get_icon(self, req):
   #    return icon(req.get_icon(), css_class=get_request_color(req, text=True))

    def active(self, obj):
        if obj.is_active:
            return icon(obj.get_icon(), css_class='green-text', tooltip=_('Active'))
        else:
            return icon(obj.get_icon(), css_class='red-text', tooltip=_('Inactive'))

    def get_customer(self, req):
        return 'test'
       #url = reverse('customers:profile', kwargs={'cust': req.customer.user.pk})
       #return '<a href="%s">%s</a>' % (url, req.customer)

    def notes_count(self, obj):
        return obj.notes.count()
    notes_count_label = _('Notes')

    def get_change_url(self, obj):
        return obj.get_absolute_url()

    class Meta:
        ordering = ('-created', '-modified')
        sortable = ('start_date', 'end_date', 'created', 'state', 'modified')
        list_filter = ('state', 'is_active')
        list_display = (
            'loan', 'state', 'start_date', 'end_date', 'current_balance',
            'operation_error', 'modified', 'created', 'notes_count', 'active'
        )

以下是与该模板相关的模型:

@python_2_unicode_compatible
class Perception(xwf_models.WorkflowEnabled, TimeStampedModel):
    loan = models.ForeignKey('loans.Loan')
    state = xwf_models.StateField(PerceptionWorkflow)
    start_date = models.DateField(_('Start date'))
    end_date = models.DateField(_('End date'), blank=True, null=True)

    current_balance = models.DecimalField(_('Current balance'),
                        default=0, decimal_places=2, max_digits=11)
    operation_error = models.SmallIntegerField(_('Operation error'), default=0)
    notes = GenericRelation(Note)

我以为我可以使用模板标签来重现这样的事情,但我想要更轻松的事情。任何人都可以告诉我在模板中可以做些什么来实现它?事实上,我的一个问题是在Django的模板中创建一个列表。

1 个答案:

答案 0 :(得分:0)

看起来最简单的方法是在视图的get_context_data方法中创建列表:

def get_context_data(self, **kwargs):
    """Add list of customer ids to the context"""
    context = super(PerceptionIndexView, self).get_context_data(**kwargs)
    customer_ids = []

    # Since this is a ListView, I believe you can change the loop to
    # 'for item in context['object_list']:', and remove the perc queryset
    perc = Perception.objects.all()
    for item in perc:
        customer_id = item.loan.reqest.customer_id
        if customer_id not in perc:
            customer_ids.append(customer_id)
    context['customer_ids'] = customer_ids
    return context

然后在您的模板中,您可以遍历列表

<ul>
{% for customer_id in customer_ids %}
    <li>customer_id</li>
{% endfor %}
</ul>