Pagination - displaying page numbers

590 views
Skip to first unread message

Stephen

unread,
Mar 23, 2006, 11:49:07 AM3/23/06
to Django users
Hello,

I've just started looking at Django, it's certainly looking like a very
good choice for the sort of projects I'll be doing in the near future.


Just now I've been looking at pagination and have a question:

I'm using the generic view "list_detail" to view a list of results. I
want to use pagination to restrict the number of items on a page. To
do this I've added a "paginate_by" value to the info_dict dictionary in
my urls.py file. This results in my results being paginated correctly,
and the additional context parameters are available in my template. I
can use these to show how many hits there were and how many pages of
results etc there are. My problem comes with trying to show a numbered
link to each page of results (like google does at the bottom of its
results page). I thought this would be straightforward - just do a
loop - but as far as I can see the {% for %} tag can only iterate over
an array, and the "pages" context property is a number. What is the
right way to go about looping from N to N+M in a template ? My feeling
is that looping over a list is the nicest way, but then I can't get the
pagination to work like I want! Apologies if I'm missing something
really obvious!

In the course of trying to figure this out I tinkered around with the
list_detail.py file and tried adding extra context parameters (e.g.
'page_list':range(1, pages+1, 1) ). These never showed up in my
templates though - are there specific steps to take in order to make
changes to the django source files be noticed by the server (I'm using
the test server that comes with Django)?

Thanks in advance for any help,

Stephen.

Stephen

unread,
Mar 23, 2006, 12:11:06 PM3/23/06
to Django users
The answer to the second part of my question *was* very obvious... I
was editing the wrong copy of the source file. I needed to change the
copy installed under my Python directory, not the one in the Django
directory.

limodou

unread,
Mar 23, 2006, 7:45:27 PM3/23/06
to django...@googlegroups.com

One way I think is just like what you do: create a extra_context.

The other way is create a custom tag to create a list. Someone has
submit this patch before. http://code.djangoproject.com/ticket/1162

Or you can use a custom tag written by me to calculate the list in
template: expr tag

http://groups.google.com/group/django-users/browse_thread/thread/6c1c162e7dd5d0d8/ce3aa7e2e3d48ae1?lnk=st&q=an+expression+tag&rnum=3#ce3aa7e2e3d48ae1

{% expr range(pages) as page_number %}
{% for i in page_number %}
...
{% endfor %}
--
I like python!
My Blog: http://www.donews.net/limodou
NewEdit Maillist: http://groups.google.com/group/NewEdit

Reply all
Reply to author
Forward
0 new messages