How to modify page and custom get variable parameters in Django 1.3 pagination


Sometimes in Django pagination, we want to adding custom variable in pagination URL. For instance, we want to build search page which need to paginate the results. In URL, it should be have “/search/?q=QUERY/” and “/search/QUERY&page=PAGE_NUMBER”. Eg:

1
2
3
http://localhost:8000/search/
http://localhost:8000/search/?q=<QUERY-HERE>
http://localhost:8000/search/?q=Hello&page=2


To build and catch GET param variable, first let we define urls.py :

1
url(r’^search/$’, ‘search’, name=’search’),

In Views.py :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def search(request):
    if request.method == ‘GET’:
       
        # Defining query set
        and_query = None
        query_set = None
       
        # Deleting page params from GET request
        params = request.GET.copy()        
        if ‘page’ in params:
            del(params[‘page’])
       
        results = MyModel.objects.all()
        paginator = Paginator(results, 10)
           
        try:
            page = int(request.GET.get(‘page’, ‘1’))
        except ValueError:
            page = 1
           
        try:
            search = paginator.page(page)
        except (EmptyPage, InvalidPage):
            search = paginator.page(paginator.num_pages)
           
        # Send GET param into pagination
        path = params.urlencode()
           
        return render_to_response(‘myapp/searc.html’, {‘search’: search, ‘path’: path })

In templates (eg: search.html)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<table>
    <thead>
        <tr>ID</tr>
        <tr>Uploader</tr>          
        <tr>Date</tr>
    </thead>
    <tbody>
        {% for data in search.object_list %}
            <tr>
                <td>{{ data.id }}</td>
                    <td>{{ data.uploader }}</td>                   
                <td>{{ data.created }}</td>
            </tr>
        {% endfor %}           
    </tbody>
</table>
   
    <div>
        {% if path %}
            <span>
                {% if search.has_previous %}
                    <a href="?{{ path }}&amp;page={{ search.previous_page_number }}">previous</a>
                {% endif %}
               
                {% for num in search.paginator.page_range %}
                    {% ifequal num search.number %}
                        <span class="current page">{{ num }}</span>
                    {% else %}
                        <a href="?{{ path }}&amp;page={{ num }}"> {{ num }}</a>
                    {% endifequal %}
                {% endfor %}
               
                {% if search.has_next %}
                    <a href="?{{ path }}&amp;page={{ search.next_page_number }}"> Next </a>
                {% endif %}
            </span>
           
        {% else %}
       
            <span>
                {% if search.has_previous %}
                    <a href="?page={{ search.previous_page_number }}">previous</a>
                {% endif %}
               
                {% for num in search.paginator.page_range %}
                    {% ifequal num search.number %}
                        <span class="current page">{{ num }}</span>
                    {% else %}
                        <a href="?page={{ num }}"> {{ num }}</a>
                    {% endifequal %}
                {% endfor %}
               
                {% if search.has_next %}
                    <a href="?page={{ search.next_page_number }}"> Next </a>
                {% endif %}
            </span>    
        {% endif %}

    </div>

Now can have pagination urls with “?page=PAGE_NUMBER” and your custom parameter. 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.