2017-12-24 13 views
0

私が悩んでいる質問。リストビューテンプレートのdjango countクエリーセットフィールド

私はあなたが私の質問の詳細が必要な場合、これは私の最初の投稿ですとして私が知っていると、それを探してくださいアクティブに等しいvenlist.Statusをカウントし、一部のHTMLタグ

でそれを表示したいですライブラリ・フロム約4時間

Models.py

VendorCH= [ 
    ('Active', 'Active'), 
    ('BlackList', 'BlackList'), 
] 

class VendorModel(models.Model): 
    Name=models.CharField(unique=True,max_length=40) 
    President=models.CharField(unique=True,max_length=40) 
    Status=models.CharField(max_length=40,choices=VendorCH,default='Active') 

Forms.py

class VendorForm(forms.ModelForm): 
    class Meta: 
     model = VendorModel 
     exclude=['VendorStatus'] 

views.py

class VendorCreateView(SuccessMessageMixin,CreateView): 
    template_name = "ePROC/Administration/Vendor_Create.html" 
    model=VendorModel 

    fields=['Name','President'] 
    success_message = "Record was created successfully" 
    success_url = reverse_lazy('ePROC:ePROCHOME') 

    def form_valid(self, form): 
     form.instance.Status = 'Active' 
     return super(VendorCreateView, self).form_valid(form) 

List_Template.html

<table class="table table-hover"> 
<thead> 
    <th> Vendor Name</th> 
    <th> President</th> 
    <th> Status</th>           
</thead> 
<tbody> 
{% for venlist in object_list %} 
    <tr> 
     <td>{{ venlist.VendorName }}</td> 
     <td>{{ venlist.President}}</td> 
     <td>{{ venlist.Status}}</td>          
    </tr> 
{% endfor %} 
<tbody> 
</table> 

答えて

0
from django.views.generic.list import ListView 

class VenderListView(ListView): 
    queryset=VendorModel.objects.all() 

    def get_context_data(self, **kwargs): 
     context = super().get_context_data(**kwargs) 
     context['total_active_records'] = self.queryset.filter(Status='Active').count() 
     return context 

List_Template.html:

<div>Total Active Vendors: <span> {{ total_active_records }}</span></div> 
0

これはUmar Asghar's answerに基づいて、私のために働きました。

class IndexListView(ListView): 
    template_name = "ePROC/Base/index.html" 
    model = CreateReqModel 
    context_object_name = 'reqlists' 
    queryset=CreateReqModel.objects.all() 
    paginate_by = 10 

    def get_context_data(self, **kwargs): 
     context = super(IndexListView, self).get_context_data(**kwargs) 
     context['total_active_records'] = self.queryset.filter(Status='Initiate').count() 
     context['range'] = range(context["paginator"].num_pages) 
     return context