在 Django 分页中 Paginate 不起作用

发布于 2024-09-05 15:58:04 字数 3733 浏览 6 评论 0原文

问题:如何强制分页正确工作?问题是 {% paginate %} 不起作用,但其他 {% load pagination_tags %}{% autopaginate object_list 10 %}作品!

当我将 {% paginate %} 添加到 html 页面时,出现错误消息:

TemplateSyntaxError at /logging
Caught an exception while rendering: pagination/pagination.html

我所做的:

  1. 安装 django-pagination 没有任何问题。当我在 python import pagination 中执行此操作时,它运行良好。

  2. 在settings.py中为INSTALLED_APP添加了分页:

    已安装的应用程序 = ( # ..., '分页', )

  3. 在settings.py中添加:

    模板上下文处理器 = ( “django.core.context_processors.auth”, “django.core.context_processors.debug”, “django.core.context_processors.i18n”, “django.core.context_processors.media”, “django.core.context_processors.request” )

  4. 同时添加到settings.py中间件:

    中间件_类 = ( # ... '分页.middleware.PaginationMiddleware', )

  5. 添加到views.py顶部:

    from django.template import RequestContext

  6. 最后添加到我的HTML模板页面行:

    {% 加载分页标签 %} ... {% 自动分页 item_list 50 %} {% for item_list %} ... {% 结束 %} {% paginate %}

谢谢。


ADDED:错误报告顶部:

TemplateSyntaxError at /logging
Caught an exception while rendering: pagination/pagination.htmlRequest Method:  GET
Request URL:    http://host:8123/logging?portfolio_id=1
Exception Type: TemplateSyntaxError
Exception Value:    Caught an exception while rendering: pagination/pagination.html
Exception Location: /usr/local/lib/python2.6/dist-packages/django/template/debug.py in render_node, line 81
Python Executable:  /usr/bin/python
Python Version: 2.6.2
Python Path:    ['/home/mosg/sources/django/apm', '/usr/local/lib/python2.6/dist-packages/django_pagination-1.0.5-py2.6.egg', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/var/lib/python-support/python2.6', '/var/lib/python-support/python2.6/gtk-2.0', '/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages']
Server time:    Thu, 17 Jun 2010 06:29:45 -0500

Template error

In template /home/mosg/sources/django/apm/templates/accounting/logging.html, error at line 93
Caught an exception while rendering: pagination/pagination.html
83      <td>{{ item.transaction_datetime }}</td>
84      <td>{{ item.src_account }}</td>
85      <td>{{ item.dst_account }}</td>
86      <td>{{ item.body }}</td>
87      <td>{{ item.estimated }}</td>
88  <!--
89      <td><a href="./admin/accounting/transaction/{{item.id}}/">edit</a></td>
90  -->
91  </tr>
92  {% endfor %}
93  {% paginate %}
94  </table>
95  {% else %}
96      <p>No transaction logs are available.</p>
97  {% endif %}
98          </div>
99      
100 
101 </div>
102        
103         <br class="clear" />

stevejalimADDED

@login_required
def logging(request):
    pid = request.GET.get('portfolio_id', 1)
    item_list = TransactionsLogging.objects.filter(Q(portfolio_id=pid)).order_by('-datetime')
    return render_to_response('accounting/logging.html', {'item_list': item_list, 'user': request.user,}, context_instance = RequestContext(request))

PS:需要进行一些编辑,因为我不能在这里很好地工作 django 代码风格: )

Question: How to force pagination work correctly? The problem is that {% paginate %} does not work, but other {% load pagination_tags %} and {% autopaginate object_list 10 %} works!

Error message appeared, when I add {% paginate %} into html page:

TemplateSyntaxError at /logging
Caught an exception while rendering: pagination/pagination.html

What I have done:

  1. Install django-pagination without any problems. When I do in python import pagination, it's work well.

  2. Added pagination to INSTALLED_APP in settings.py:

    INSTALLED_APPS = (
    # ...,
    'pagination',
    )

  3. Added in settings.py:

    TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.request"
    )

  4. Also add to settings.py middleware:

    MIDDLEWARE_CLASSES = (
    # ...
    'pagination.middleware.PaginationMiddleware',
    )

  5. Add to top in views.py:

    from django.template import RequestContext

  6. And finally add to my HTML template page lines:

    {% load pagination_tags %}
    ...
    {% autopaginate item_list 50 %}
    {% for item in item_list %}
    ...
    {% endfor %}
    {% paginate %}

Thanks.


ADDED: Top of error report:

TemplateSyntaxError at /logging
Caught an exception while rendering: pagination/pagination.htmlRequest Method:  GET
Request URL:    http://host:8123/logging?portfolio_id=1
Exception Type: TemplateSyntaxError
Exception Value:    Caught an exception while rendering: pagination/pagination.html
Exception Location: /usr/local/lib/python2.6/dist-packages/django/template/debug.py in render_node, line 81
Python Executable:  /usr/bin/python
Python Version: 2.6.2
Python Path:    ['/home/mosg/sources/django/apm', '/usr/local/lib/python2.6/dist-packages/django_pagination-1.0.5-py2.6.egg', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/var/lib/python-support/python2.6', '/var/lib/python-support/python2.6/gtk-2.0', '/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages']
Server time:    Thu, 17 Jun 2010 06:29:45 -0500

Template error

In template /home/mosg/sources/django/apm/templates/accounting/logging.html, error at line 93
Caught an exception while rendering: pagination/pagination.html
83      <td>{{ item.transaction_datetime }}</td>
84      <td>{{ item.src_account }}</td>
85      <td>{{ item.dst_account }}</td>
86      <td>{{ item.body }}</td>
87      <td>{{ item.estimated }}</td>
88  <!--
89      <td><a href="./admin/accounting/transaction/{{item.id}}/">edit</a></td>
90  -->
91  </tr>
92  {% endfor %}
93  {% paginate %}
94  </table>
95  {% else %}
96      <p>No transaction logs are available.</p>
97  {% endif %}
98          </div>
99      
100 
101 </div>
102        
103         <br class="clear" />

ADDED for stevejalim:

@login_required
def logging(request):
    pid = request.GET.get('portfolio_id', 1)
    item_list = TransactionsLogging.objects.filter(Q(portfolio_id=pid)).order_by('-datetime')
    return render_to_response('accounting/logging.html', {'item_list': item_list, 'user': request.user,}, context_instance = RequestContext(request))

PS: some edits required, because I can't django code style work well here :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

方觉久 2024-09-12 15:58:04

您能提供有关 TemplateSyntaxError 的更多详细信息吗?

您的所有配置看起来都很好。
pagination/pagination.html 模板中有 {% load i18n %}。您的设置文件中是否有USE_I18N = True

Can you provide more details off TemplateSyntaxError?

All your configurations looks fine.
In pagination/pagination.html template there is {% load i18n %}. Do you have USE_I18N = True in your settings file?

用心笑 2024-09-12 15:58:04

之前找到了解决方案:django-pagination 您需要最新的版本,修复bug!

Solution was found earlier: django-pagination you need with of the latest version, which is fix the bug!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文