django和flask分页
/ / / 阅读数:2413前言
最近搞了一些关于 flask 和 django 的东西,尤其是 django 的模板和 admin 功能以及这些框架使用 bootstrap 的东西,没时间更新博客,先说一下 flask 和 django 分页吧
flask-paginate">flask 的 bootstrap 分页插件 flask-paginate
其实安装很常规,他的思路就是根据你的数据量给每个页面加一个 li 前缀到最后返回的 div 里面。因为官网提供的说明很简单,我在这里仔细说说:
- 官网说给你的网站页面添加 css:
.pagination-page-info { padding: .6em; padding-left: 0; width: 40em; margin: .5em; margin-left: 0; font-size: 12px; } .pagination-page-info b { color: black; background: #6aa6ed; padding-left: 2px; padding: .1em .25em; font-size: 150%; }
@app.route('/') def index(): pagesize = 100 #设定每页显示条目数 page = int(request.args.get('page',0)) #获取当前页面页数 data = get_MongoData(page, pagesize) #get_MongoData是我自己的函数,根据页数过滤要显示的数据(因为实在太大了) pagination = Pagination(total=data[1], per_page=pagesize, page=page) #total的值是总数据条目,per_page表示每页显示数目,page就是当前页数。还可以设置向前/后页面标签(默认是<>>)等 return render_template("index.html", pagination=pagination)
我对他的一点修改:
- 我发现在我的程序里面,这个分页栏在后部会放不下而换行显示,我就直接把 link_css 制定的 div 改成了行内元素 span
当我默认使用 link_size, 代码是这样:
link_css = ' - '
其实最后页面出来的效果是 '
- '
这样就没有符合的 bootstrap 类,所以我修改了 links 方法:
@property def links (self): '''get all the pagination links''' if self.total_pages <= 1: return '' if not self.link_size: self.link_size = '' s = [link_css.format (self.link_size)] s.append (self.prev_page) for page in self.pages: s.append (self.single_page (page) if page else gap_marker) s.append (self.next_page) s.append ('') return ''.join (s)
django-bootstrap-pagination">flask 的 bootstrap 分页插件 django-bootstrap-pagination
django 的插件比较复杂,它自己定义了中间件和标签,这样你需要在模板中 load 它提供的函数,并且很 nb 的使用了 RequestContext 去处理变量,可以看张沈鹏以前写的一个小文章: django 简化 view 函数的编写
- 先看我的后台方法:
def showlist(req): t = req.GET.get('type', None) l = req.GET.get('app', None) if t and l: db = getMongo('XXX.XXX.XXX.XXX:XX', 'dc2') if t == 'v': q = re.compile(r'.*%s$' % l) data = db.site.find({'modules.site.level':'v4', 'site':{ '$regex' : q }}, {'site':1, '_id':0, 'modules.site.links':1, 'modules.site.keywords':1}).sort( 'modules.site.site.check_time') return render_to_response("list.html", {'data':data}, context_instance=RequestContext(req))
但是运行时候会报错:
Traceback: File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) File "/home/dongwm/centerCon/views.py" in showlist 68. return render_to_response("list.html", {'data':data}, context_instance=RequestContext(req)) File "/usr/local/lib/python2.6/dist-packages/django/shortcuts/__init__.py" in render_to_response 20. return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) File "/usr/local/lib/python2.6/dist-packages/django/template/loader.py" in render_to_string 176. return t.render(context_instance) File "/usr/local/lib/python2.6/dist-packages/django/template/base.py" in render 140. return self._render(context) File "/usr/local/lib/python2.6/dist-packages/django/template/base.py" in _render 134. return self.nodelist.render(context) File "/usr/local/lib/python2.6/dist-packages/django/template/base.py" in render 823. bit = self.render_node(node, context) File "/usr/local/lib/python2.6/dist-packages/django/template/debug.py" in render_node 74. return node.render(context) File "/home/dongwm/centerCon/templatetags/pagination_tags.py" in render 91. page_obj = paginator.page(context['request'].page) File "/usr/local/lib/python2.6/dist-packages/django/template/context.py" in __getitem__ 54. raise KeyError(key) Exception Type: KeyError at /showlist/ Exception Value: 'request'
不管你用那个插件都会有这个报错。。。
后来发现原因是:
settings 文件没有设置 TEMPLATE_CONTEXT_PROCESSORS理由:模板上下文处理器会指定了哪些 contextprocessors 总是默认被使用。这样就省去了每次使用 RequestContext 都指定 processors 的麻烦 在 settings 加入: TEMPLATE_CONTEXT_PROCESSORS = ( "django.core.context_processors.media", "django.core.context_processors.request" )