Question

I've been trying to construct my own tag for pagination - pagetag.

But when I render this:

        {% if people.has_previous %}
            <li><a href={% pagetag people.previous_page_number %}>&laquo;</a></li>
        {% else  %}
            <li class="disabled"><a href="#">&laquo;</a></li>
        {% endif %}

        {% for page in people.paginator.page_range  %}
            {% if page == people.number %}
                <li class="disabled"><a href="#">{{page}}</a></li>
            {% else %}
                <li><a href={% pagetag page %}>{{page}}</a></li>
            {% endif %}
        {% endfor %}

        {% if people.has_next %}
            <li><a href={% pagetag people.next_page_number %}>&raquo;</a></li>
        {% else %}
            <li class="disabled"><a href="#">&raquo;</a></li>
        {% endif %}

I get folowing links on first page:

            <li class="disabled"><a href="#">&laquo;</a></li>
                <li class="disabled"><a href="#">1</a></li>
                <li><a href=/?page=page>2</a></li>
                <li><a href=/?page=page>3</a></li>
               <li><a href=/?page=people.next_page_number>&raquo;</a></li>

Not the desired ?page=2 and so on.

def pagetag(parser, token):
    tokens = token.split_contents()
    print tokens
    if len(tokens) < 1:
        raise TemplateSyntaxError, "pagetag takes at least 1 arguments"
    return PageNode(tokens[1].strip())

How to pass the arguments correctly?

Was it helpful?

Solution

Use simple tags. For example:

@register.simple_tag
def pagetag(page):
    return '/?page=' + page

Then, in your view, make sure to use {{ }} instead of {% %}:

<li><a href="{{ pagetag page }}">{{page}}</a></li>

Note: It's not recommended to use a different tag just for prepending a simple string. Just do it like in the documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top