python - 分页在 Django 模板页面中不起作用
问题描述
我试图在模板页面中包含分页。但是在执行项目时遇到了模板语法错误。
它说: 无法解析剩余部分:'==i' from 'posts.number==i'
我对此感到非常沮丧。
视图.py
def posts(request):
posts = Post.objects.filter(active=True)
myFilter = PostFilter(request.GET, queryset=posts)
posts = myFilter.qs
page = request.GET.get('page')
paginator = Paginator(posts, 3)
try:
posts = paginator.page(page)
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = paginator.page(paginator.num_pages)
context = {'posts': posts, 'myFilter': myFilter}
return render(request, 'base/posts.html', context)
帖子.html
<div class="row">
{%if posts.has_other_pages%}
<ul class="pagination">
{%for i in posts.paginator.page_range%}
{%if posts.number==i%}
<li class="page-item"><a class="active page-link">{{i}}</a></li>
{%else%}
<li class="page-item"><a href="?page={{i}}" class="page-link">{{i}}</a></li>
{%endif%}
{%endfor%}
</ul>
{%endif%}
</div>
解决方案
posts.number
请在和之间添加空格i
。我建议这样的事情:
{%if posts.number == i%}
而不是{%if posts.number==i%}
<div class="row">
{%if posts.has_other_pages%}
<ul class="pagination">
{%for i in posts.paginator.page_range%}
{%if posts.number == i%}
<li class="page-item"><a class="active page-link">{{i}}</a></li>
{%else%}
<li class="page-item"><a href="?page={{i}}" class="page-link">{{i}}</a></li>
{%endif%}
{%endfor%}
</ul>
{%endif%}
</div>
推荐阅读
- ios - 如何快速添加按钮以访问照片库到键盘?
- python - Python:交叉导入模块失败
- c++ - 如何使用自定义重复判断功能进行设置?
- jestjs - react-testing-library中的每次测试后如何清理fireEvents?
- javascript - 使用过滤器功能搜索连接数组
- java - 如何在 jtextbox 上实时验证正则表达式?
- php - 如何在 PHP 中引用数据结构作为 sprintf 的参数?
- php - 不会发送电子邮件
- php - PHP:运行“codeception run wpunit”时出现类加载问题
- python-3.x - 无法导入名称“balanced_accuracy_score”