我想显示一个选择字段的总数,所以基本上我做到了这一点,这是可行的,但不是给我总数就是重新填充字段。
而且必须如下所示:未开始:5或仅5
有人知道如何解决这个问题吗?
views.py
class UserPostListView(ListView):
model = Post
template_name = 'blog/user_posts.html'
context_object_name='posts'
paginate_by = 15
def get_queryset(self):
user = get_object_or_404(User, username=self.kwargs.get('username'))
return Post.objects.filter(author=user).order_by('-date_posted')
def get_context_data(self, **kwargs):
context = super(UserPostListView, self).get_context_data(**kwargs)
user = get_object_or_404(User, username=self.kwargs.get('username'))
context['postuser'] = Post.objects.filter(author=user).order_by('-date_posted')[:1]
context['posts'] = Post.objects.filter(author=user).order_by('-date_posted')
context['postns'] = Post.objects.filter(author=user,status="NOT STARTED").annotate(dcount=Count('status')).order_by('status')
return context
的HTML
{% for post in postns %}
<div class="col-auto col-ns-st padding-col-st"><span class="vertical-span"><i class="fa fa-circle color-ico-st"></i> {{ post.status }}</span></div>
{% endfor %}
models.py
class Post(models.Model):
NS= 'NOT STARTED'
STATUS_CHOICES = (
('NOT STARTED','NOT STARTED'),
('IN PROGRESS','IN PROGRESS'),
('COMPLETE','COMPLETE'),
)
title=models.CharField(max_length=100)
content=models.TextField(blank=True)
author=models.ForeignKey(User, on_delete=models.CASCADE)
status=models.CharField(max_length=40, choices = STATUS_CHOICES,default=NS)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk':self.pk})
def __str__(self):
return 'Post: {}'.format(self.status)