2017-04-11 1 views
0
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') 

このようなループをテンプレートに再現したいと思います。ここでDjangoのテンプレートで再現

は、そのテンプレートに関連した図である。

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) 

私はそのようなことを再現するtemplatetagを使用できると思ったが、私はもっ​​と簡単にしたいです。誰でも私のテンプレートに実装するために何ができるのか教えていただけますか?実際、私の問題の1つは、Djangoのテンプレート内にリストを作成することです。

+0

をループすることができます'perc'変数?あなたはテンプレートのリストで何をしたいですか? – Alasdair

+1

あなたは 'user = item.loan.request.customer.user'を必死にしています – thebjorn

+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> 
+0

'TypeError:**の後のget_context_data()引数はタプルではなくマッピングでなければならない' ...なぜこのエラーがありますか? –

+0

typoがありました。メソッド定義で '** kwargs'にする必要があります。 – Alasdair

関連する問題