为什么这个 (Django) 'filter' 函数不起作用?
Why is this (Django) 'filter' function not working?
我需要帮助来弄清楚为什么过滤不起作用。它一直有效,直到我决定使用 Django official docs and code comes from here 添加分页。)我在 Django shell 中测试了查询结果(按照文档中的步骤)并调整了代码。分页本身会显示,但不是每页显示 5 个项目(如 paginator = Paginator(gem_list, 5)
行中指定的那样,所有项目都会显示——并且每个页面上都相同。所以我很困惑问题是否出在使用 filter
部分,或使用模板。
#views.py
def gem_list(request):
# gem_list = Rhyme.objects.filter(is_gem=True)
rhyme_list = Rhyme.objects.all()
objects= list(rhyme_list) #converting the queryset to a list
gem_list = objects.filter(is_gem=True)
paginator = Paginator(gem_list, 5)
page = request.GET.get('page', 1)
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
page_range = paginator.get_elided_page_range(number=page)
try:
page_obj = paginator.get_page(page_number)
except PageNotAnInteger:
page_obj = paginator.page(1)
except EmptyPage:
page_obj = paginator.page(paginator.num_pages)
context = {
'page_obj': page_obj,
'gem_list' : gem_list,
'paginator' : paginator,
'page' : page,
}
return render(request, 'rhymes/gem_list.html', context)
#template:
<div class="container">
{% for gem in gem_list %}
<p class="rhyme_list"> <a href="{{ gem.get_absolute_url }}">{{gem}}</a></p>
{% endfor %}
<ul class="pagination justify-content-center flex-wrap mt-3 mb-4">
{% if page_obj.has_previous %}
<li class="page-item"><a class="page-link" href="?page=1">First</a></li>
<li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a></li>
{% else %}
<li class="disabled page-item"><span class="page-link">Previous</span></li>
{% endif %}
{% for i in page_range|default_if_none:page_obj.paginator.get_elided_page_range %}
{% if page_obj.number == i %}
<li class="active page-item"><span class="page-link">{{ i }} <span class="sr-only">(current)</span></span>
</li>
{% else %}
{% if i == page_obj.paginator.ELLIPSIS %}
<li class="page-item"><span class="page-link">{{ i }}</span></li>
{% else %}
<li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a></li>
<li class="page-item"><a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">Last</a></li>
{% else %}
<li class="disabled page-item"><span class="page-link">»</span></li>
{% endif %}
</ul>
#models.py
class Rhyme(models.Model):
rhyme = models.TextField(max_length=120)
create_date = models.DateTimeField(auto_now_add=True)
update_date = models.DateTimeField(auto_now=True)
is_gem = models.BooleanField(default=False)
contributor = models.ForeignKey(User, related_name="contributor", on_delete=models.CASCADE)
class Meta:
verbose_name_plural = "rhymes"
def __str__(self):
return self.rhyme
@property
def num_rhymes(self):
return Rhyme.objects.all.count()
@property
def contributor_rhyme_count(self):
return Rhyme.objects.filter(rhymes=self).count()
def get_absolute_url(self):
return reverse('rhyme_detail', args=[str(self.id)])
def get_absolute_url(self):
return "/rhymes/{}/".format(self.pk)
# Metadata
class Meta:
ordering = ['rhyme']
您必须显示页面对象 (page_obj
) 而不是完整的 gem_list:
{% for gem in page_obj %}
<p class="rhyme_list"> <a href="{{ gem.get_absolute_url }}">{{gem}}</a></p>
{% endfor %}
我需要帮助来弄清楚为什么过滤不起作用。它一直有效,直到我决定使用 Django official docs and code comes from here 添加分页。)我在 Django shell 中测试了查询结果(按照文档中的步骤)并调整了代码。分页本身会显示,但不是每页显示 5 个项目(如 paginator = Paginator(gem_list, 5)
行中指定的那样,所有项目都会显示——并且每个页面上都相同。所以我很困惑问题是否出在使用 filter
部分,或使用模板。
#views.py
def gem_list(request):
# gem_list = Rhyme.objects.filter(is_gem=True)
rhyme_list = Rhyme.objects.all()
objects= list(rhyme_list) #converting the queryset to a list
gem_list = objects.filter(is_gem=True)
paginator = Paginator(gem_list, 5)
page = request.GET.get('page', 1)
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
page_range = paginator.get_elided_page_range(number=page)
try:
page_obj = paginator.get_page(page_number)
except PageNotAnInteger:
page_obj = paginator.page(1)
except EmptyPage:
page_obj = paginator.page(paginator.num_pages)
context = {
'page_obj': page_obj,
'gem_list' : gem_list,
'paginator' : paginator,
'page' : page,
}
return render(request, 'rhymes/gem_list.html', context)
#template:
<div class="container">
{% for gem in gem_list %}
<p class="rhyme_list"> <a href="{{ gem.get_absolute_url }}">{{gem}}</a></p>
{% endfor %}
<ul class="pagination justify-content-center flex-wrap mt-3 mb-4">
{% if page_obj.has_previous %}
<li class="page-item"><a class="page-link" href="?page=1">First</a></li>
<li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a></li>
{% else %}
<li class="disabled page-item"><span class="page-link">Previous</span></li>
{% endif %}
{% for i in page_range|default_if_none:page_obj.paginator.get_elided_page_range %}
{% if page_obj.number == i %}
<li class="active page-item"><span class="page-link">{{ i }} <span class="sr-only">(current)</span></span>
</li>
{% else %}
{% if i == page_obj.paginator.ELLIPSIS %}
<li class="page-item"><span class="page-link">{{ i }}</span></li>
{% else %}
<li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a></li>
<li class="page-item"><a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">Last</a></li>
{% else %}
<li class="disabled page-item"><span class="page-link">»</span></li>
{% endif %}
</ul>
#models.py
class Rhyme(models.Model):
rhyme = models.TextField(max_length=120)
create_date = models.DateTimeField(auto_now_add=True)
update_date = models.DateTimeField(auto_now=True)
is_gem = models.BooleanField(default=False)
contributor = models.ForeignKey(User, related_name="contributor", on_delete=models.CASCADE)
class Meta:
verbose_name_plural = "rhymes"
def __str__(self):
return self.rhyme
@property
def num_rhymes(self):
return Rhyme.objects.all.count()
@property
def contributor_rhyme_count(self):
return Rhyme.objects.filter(rhymes=self).count()
def get_absolute_url(self):
return reverse('rhyme_detail', args=[str(self.id)])
def get_absolute_url(self):
return "/rhymes/{}/".format(self.pk)
# Metadata
class Meta:
ordering = ['rhyme']
您必须显示页面对象 (page_obj
) 而不是完整的 gem_list:
{% for gem in page_obj %}
<p class="rhyme_list"> <a href="{{ gem.get_absolute_url }}">{{gem}}</a></p>
{% endfor %}