我正在尝试对URL中的page参数进行分页(而不是GET参数)。我还希望我的分页能够在多个不同的模板中共享代码。
鉴于此,我认为我需要做这样的事情:
urls.py:
url(r'^alias/page;(?P<page>[0-9]+)/(?P<id>.*)$', alias.get, name="alias"),
tempaltes / alias.html:
<div>...stuff...</div>
{% include "paginator.html" %}
templates / paginator.html:
{% if page_obj.has_previous or page_obj.has_next %}
{% load filters %}
<div class="pagination clear">
{% if page_obj.has_previous %}
<a href="{% url somemagic %}" class="prev">‹‹ previous</a>
...
什么是somemagic
?
假设除了设置页面page_obj.previous_page_number
答案 0 :(得分:2)
修改强>
您需要 somemagic
作为具有当前视图名称的变量。
试试这个:
{% with request.path_info|resolve_url_name as current_view %}
{% url current_view page_obj.previous_page_number object.id %}
{% endwith %}
你可以使用django-snippets的一些代码来实现这个目的:
resolve_to_name(path)
返回path
的视图名称。您只需要创建一个使用此功能的过滤器。此解决方案不适用于以下网址:
'alias/param1_regexp/param2_regexp/page;(?P<page>[0-9]+)/(?P<id>.*)$'
因为你对param1和param2一无所知。
可以对上面的django-snippets进行修改,以使这种网址有效:
第一个代码段修改:
from django.template import defaulttags, VariableDoesNotExist, Variable
class ResolvingURLNode(defaulttags.URLNode):
def render(self, context):
original_view_name = self.view_name
try:
resolved = Variable(self.view_name).resolve(context)
if len(resolved) > 1:
self.view_name = resolved[0]
if resolved[1]:
self.args = [Variable(arg) for arg in resolved[1]]
elif len(resolved) > 0:
self.view_name = resolved[0]
else:
self.view_name = resolved
except VariableDoesNotExist:
pass
ret = super(defaulttags.URLNode, self).render(context)
# restore view_name in case this node is reused (e.g in a loop) in
# which case the variable might resolve to something else in the next iteration)
self.view_name = original_view_name
return ret
defaulttags.URLNode = ResolvingURLNode
第二段代码修改
from django.core.urlresolvers import RegexURLResolver, RegexURLPattern, Resolver404, get_resolver
__all__ = ('resolve_to_name',)
def _pattern_resolve_to_name(self, path):
match = self.regex.search(path)
if match:
name = ""
if self.name:
name = self.name
elif hasattr(self, '_callback_str'):
name = self._callback_str
else:
name = "%s.%s" % (self.callback.__module__, self.callback.func_name)
if len(match.groups()) > 0:
groups = match.groups()
else:
groups = None
return name, groups
def _resolver_resolve_to_name(self, path):
tried = []
match = self.regex.search(path)
if match:
new_path = path[match.end():]
for pattern in self.url_patterns:
try:
resolved = pattern.resolve_to_name(new_path)
if resolved:
name, groups = resolved
else:
name = None
except Resolver404, e:
tried.extend([(pattern.regex.pattern + ' ' + t) for t in e.args[0 ['tried']])
else:
if name:
return name, groups
tried.append(pattern.regex.pattern)
raise Resolver404, {'tried': tried, 'path': new_path}
# here goes monkeypatching
RegexURLPattern.resolve_to_name = _pattern_resolve_to_name
RegexURLResolver.resolve_to_name = _resolver_resolve_to_name
def resolve_to_name(path, urlconf=None):
return get_resolver(urlconf).resolve_to_name(path)
基本上,resolve_to_name返回视图的名称及其参数作为元组, new {% url myvar %}
接受此元组并使用它来反转视图名称的路径,它是参数。
如果您不喜欢过滤器方法,也可以使用自定义中间件来完成。
上一个回答
你应该检查django-pagination,它是一个非常好的django应用程序,易于使用并完成工作。
使用django分页,对可迭代分页的代码将是:
{% load pagination_tags %}
{% autopaginate myiterable 10 %} <!-- 10 elements per page -->
{% for item in myiterable %}
RENDERING CONTENT
{% endfor %}
{% paginate %} <!-- this renders the links to navigate through the pages -->
myiterable 可以是任何可迭代的:list,tuple,queryset等
googlecode的项目页面: http://code.google.com/p/django-pagination/
答案 1 :(得分:0)
它将类似于以下内容。除了我不知道id是什么意思所以我只是放了一个通用的对象id。 url的语法是{%url view_name param1 param2 ...%}
{% url alias page_obj.previous_page_number object.id %}
根据您的需要更新:
{% url alias page_obj.previous_page_number object.id as prev_url %}
{% include "paginator.html" %}
...
{% if page_obj.has_previous %}
<a href="{{prev_url}}" class="prev">‹‹ previous</a>