Django模板标签异常 [英] Django template tag exception

查看:37
本文介绍了Django模板标签异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来这个模板标签对大多数人来说就像一个魅力:

http://blog.localkinegrinds.com/2007/09/06/digg-style-pagination-in-django/

出于某种原因,我收到此错误:渲染时捕获异常:'is_paginated'

我在我的模板中使用这个模板标签,如下所示:

{% 加载 digg_paginator %}{% digg_paginator %}

其中 digg_paginator.py 在我的 app/templatetags 文件夹中,包含的模板上下文 digg_paginator.html 在我的 app/templates 文件夹中.需要分页的查询集称为目的地".如果我只指定{% digg_paginator %},它怎么知道要分页的变量?我觉得我在这里遗漏了一些重要的东西或者只是愚蠢的:P

请有人帮忙,或向我解释应该如何做到这一点.

解决方案

如果您阅读该模板标签的描述,它会说它是为使用内置的 通用视图:

<块引用><块引用>

与 PaginatorTag 一样,这个标签是一个非常基本的包含标签,它建立在使用通用 object_list 视图进行分页时已经在上下文中设置的变量上.还创建了一些额外的上下文变量...

is_paginated 变量由通用视图设置(查看之前链接的 django 文档的 template_context 部分).

更新

要使用该分页器,您需要修改视图以使用 Django 中的通用视图功能.由于您尚未提供视图或网址的代码,因此我无法就如何更改代码给您任何具体建议.

如果您遵循 Django 教程第 4 部分(最后一部分)他们将引导您修改您在前几节使用通用视图.使用通用视图的优势在于它会自动处理大部分脑死的 index() 和 detail() 视图代码:

<块引用><块引用>

这些视图代表了基本 Web 开发的一个常见情况:根据 URL 中传递的参数从数据库中获取数据,加载模板并返回呈现的模板.因为这很常见,Django 提供了一个快捷方式,称为通用视图"系统.

It looks like this template tag works like a charm for most people:

http://blog.localkinegrinds.com/2007/09/06/digg-style-pagination-in-django/

For some reason I get this error: Caught an exception while rendering: 'is_paginated'

I use this template tag in my template like so:

{% load digg_paginator %}
{% digg_paginator %}

Where digg_paginator.py is in my app/templatetags folder and the included template context digg_paginator.html is in my app/templates folder. The queryset that needs pagination is called 'destinations'. If i just specify {% digg_paginator %}, how does it know what variable to paginate? I feel I am missing something important here or just plain stupid :P

Someone please help, or explain to me how this should be done.

解决方案

If you read the description of that template tag, it says it is built to paginate using the built-in generic views:

Like the PaginatorTag, this tag is a very basic inclusion tag that builds on the variables already set on the context when paginating with the generic object_list view. There are a few additional context variables created...

The is_paginated variable is setup by the generic view (look at the template_context section of the django documentation linked earlier).

Updated

To use that paginator, you will need to modify your view to use the generic view functionality in Django. Since you haven't provided the code for your views or urls I can't give you any concrete suggestions on how to change your code.

If you follow through the Django Tutorial, in part 4 (the last section) they walk you through modifying the simple application that you built in the previous sections to use generic views. The strength of using generic views is that it automatically handles most of the brain dead index() and detail() view code:

These views represent a common case of basic Web development: getting data from the database according to a parameter passed in the URL, loading a template and returning the rendered template. Because this is so common, Django provides a shortcut, called the "generic views" system.

这篇关于Django模板标签异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆