urls.py
from django.views.generic import list_detail
set=Post.objects.all().order('-date')
#hack _clone() method
try :
set._clone()
except AttributeError :
set._clone=lambda:set
#hack model property
try :
set.model
except AttributeError :
set.model=Post
post_list_info = {
'queryset' : set,
'allow_empty': True,
'paginate_by':6,
'extra_context':{'users':users,},
}
urlpatterns += patterns('',
(r'^posts/$', list_detail.object_list, post_list_info),
(r'^posts/(?P<page>\d+)$', list_detail.object_list, post_list_info),
)
发一个Generic View: List_detail 的hack ,这样就可以用了。觉得这样hack太暴力了 就没有用...贴出来也许有人需要吧:-)
我自己对需要分页的list 写了个公共函数:
#其实能整成decorator的形式 就不整了>_<!
def list_generic(request, page=1, object_list=[]):
page =int(page)
paginator=Paginator(object_list, 20, orphans=5)
page_obj=paginator.page(page)
dict = {'posts':page_obj.object_list,
'is_paginated': page_obj.has_other_pages(),
'results_per_page': paginator.per_page,
'has_next': page_obj.has_next(),
'has_previous': page_obj.has_previous(),
'page': page_obj.number,
'next': page_obj.next_page_number(),
'previous': page_obj.previous_page_number(),
'first_on_page': page_obj.start_index(),
'last_on_page': page_obj.end_index(),
'pages': paginator.num_pages,
'hits': paginator.count,
'page_range': paginator.page_range,
'users':users,
}
return render_to_response('blog/list_post.html', dict)
使用方法:
#分类函数
def catalog(request,catalog,page=1):
page =int(page)
catalog = force_unicode(urllib.unquote(smart_str(catalog)))
object_list=Post.all().filter("catalog =", catalog).order('-date')
return list_generic(request, page, object_list)
trackback=http://ihere.appspot.com/post/2483