generic views下的分页 作者:zjxplq发表于2009-05-24 22:43:10
使用generic views,可以不必写views函数
[b]urls.py[/b][code]from django.views.generic import list_detail
from mytest.books.models import Publisher
urlpatterns = patterns('django.views.generic.list_detail',
url(r'^publishers/?$','object_list',{
'queryset': Publisher.objects.all(),
'template_object_name': 'publisher',
'template_name':'books/publisher_list.html',
'paginate_by':2,
},name='publishers_list'),
)
[/code]在模板中的变化:
如果在urls.py中不设置'template_object_name': 'publisher',则模板中使用object_list来获得对象列表;如设置了,则此处可使用publisher_list来获得对象列表
[b]publisher_list.html[/b][code]{% if has_previous %}
上一页
{% endif %} {% if has_next %}
下一页
{% endif %}
Publishers
- {{ publisher.name }}
{% for publisher in publisher_list %}
{% endfor %}
1)url:r'^publishers/?$'中的[color=Red]"/?"[/color]不能缺
2)模板中关于分页部分上一页中"[b][color=Red]?[/color][/b]"之前没有"[b][color=Red]/[/color][/b]"
[[i] 本帖最后由 zjxplq 于 2009-5-24 22:46 编辑 [/i]]
回复主题
