Django 如何在基于 class 的视图分页中传递用户对象?
Django how to pass user object in class based view pagenation?
默认的class 基于视图分页返回所有对象。我有一个我的每个用户的帐户页面,我需要在其中显示页码。默认情况下,所有用户的分页都是相同的。让你稍微解释一下。如果用户 A 有 10 个项目,我每页列出 2 个项目,那么用户 A 将在他的帐户中看到总共 5 页,这很好,但用户 B 没有任何项目,他目前只看到分页编号,如 page1、page2、page3 ...在他的帐户中,因为当前分页从数据库返回所有项目。这是我的代码:
views.py
class BlogMyAccount(ListView):
paginate_by = 10
model = Blog
template_name = 'blog/my-account.html'
ordering = ['-id']
#html
{% for blog in object_list %}
{% if user.id == blog.author.id %}
....my code
{% endif %}
{% endfor %}
<!---pagination code---->
<ul class="pagination justify-content-center mb-4">
{% if page_obj.has_previous %}
<li class="page-item"><a class="page-link" href="?page=1">First Page</a></li>
<li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">← Back</a></li>
{% endif %}
{% if page_obj.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">Next Page →</a></li>
{% endif %}
{% for i in paginator.page_range %}
{% if page_obj.number == i %}
<li class="page-item"><a class="page-link" href="#!">{{ i }}</a></li>
{% elif i > page_obj.number|add:'-3' and i < page_obj.number|add:'3' %}
<li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
{%endif%}
{% endfor %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">Last Page</a></li>
</ul>
在基于函数的视图中,我在分页中像这样传递用户对象:
notifications = Notifications.objects.all().filter(user=user).order_by('-date')
paginator = Paginator(notifications, 20) # Show 25 contacts per page.
我试图通过在 classed 视图中使用上下文来应用相同的方法,但没有成功。在基于 class 的视图中,所有用户的分页都是相同的。
您需要像在 FBV 中那样过滤查询集。将 get_queryset() 覆盖为仅 return 与您的请求用户关联的对象。
class BlogMyAccount(ListView):
paginate_by = 10
model = Blog
template_name = 'blog/my-account.html'
ordering = ['-id']
def get_queryset(self):
queryset = Blog.objects.filter(author=self.request.user)
return queryset
另外你不需要在 Notifications.objects 上调用 .all(),它已经是一个查询集,Notifications.objects.filter() 没问题。
如果可能,将排序移动到 models.py 文件,如:
class Meta:
ordering = ['-id']
您可以覆盖 get_querset
方法,并通过使用 super()
调用,让 Django 为我们执行 .order_by
子句:
from django.contrib.auth.mixins import LoginRequiredMixin
class BlogMyAccount(<strong>LoginRequiredMixin</strong>, ListView):
paginate_by = 10
model = Blog
template_name = 'blog/my-account.html'
ordering = ['-id']
def get_queryset(self, *args, **kwargs):
# ↓ will take care of the ordering
return super().get_querset(*args, **kwargs).filter(
author=self.request.user
)
Note: You can limit views to a class-based view to authenticated users with the
LoginRequiredMixin
mixin [Django-doc].
默认的class 基于视图分页返回所有对象。我有一个我的每个用户的帐户页面,我需要在其中显示页码。默认情况下,所有用户的分页都是相同的。让你稍微解释一下。如果用户 A 有 10 个项目,我每页列出 2 个项目,那么用户 A 将在他的帐户中看到总共 5 页,这很好,但用户 B 没有任何项目,他目前只看到分页编号,如 page1、page2、page3 ...在他的帐户中,因为当前分页从数据库返回所有项目。这是我的代码:
views.py
class BlogMyAccount(ListView):
paginate_by = 10
model = Blog
template_name = 'blog/my-account.html'
ordering = ['-id']
#html
{% for blog in object_list %}
{% if user.id == blog.author.id %}
....my code
{% endif %}
{% endfor %}
<!---pagination code---->
<ul class="pagination justify-content-center mb-4">
{% if page_obj.has_previous %}
<li class="page-item"><a class="page-link" href="?page=1">First Page</a></li>
<li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">← Back</a></li>
{% endif %}
{% if page_obj.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">Next Page →</a></li>
{% endif %}
{% for i in paginator.page_range %}
{% if page_obj.number == i %}
<li class="page-item"><a class="page-link" href="#!">{{ i }}</a></li>
{% elif i > page_obj.number|add:'-3' and i < page_obj.number|add:'3' %}
<li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
{%endif%}
{% endfor %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">Last Page</a></li>
</ul>
在基于函数的视图中,我在分页中像这样传递用户对象:
notifications = Notifications.objects.all().filter(user=user).order_by('-date')
paginator = Paginator(notifications, 20) # Show 25 contacts per page.
我试图通过在 classed 视图中使用上下文来应用相同的方法,但没有成功。在基于 class 的视图中,所有用户的分页都是相同的。
您需要像在 FBV 中那样过滤查询集。将 get_queryset() 覆盖为仅 return 与您的请求用户关联的对象。
class BlogMyAccount(ListView):
paginate_by = 10
model = Blog
template_name = 'blog/my-account.html'
ordering = ['-id']
def get_queryset(self):
queryset = Blog.objects.filter(author=self.request.user)
return queryset
另外你不需要在 Notifications.objects 上调用 .all(),它已经是一个查询集,Notifications.objects.filter() 没问题。
如果可能,将排序移动到 models.py 文件,如:
class Meta:
ordering = ['-id']
您可以覆盖 get_querset
方法,并通过使用 super()
调用,让 Django 为我们执行 .order_by
子句:
from django.contrib.auth.mixins import LoginRequiredMixin
class BlogMyAccount(<strong>LoginRequiredMixin</strong>, ListView):
paginate_by = 10
model = Blog
template_name = 'blog/my-account.html'
ordering = ['-id']
def get_queryset(self, *args, **kwargs):
# ↓ will take care of the ordering
return super().get_querset(*args, **kwargs).filter(
author=self.request.user
)
Note: You can limit views to a class-based view to authenticated users with the
LoginRequiredMixin
mixin [Django-doc].