Asterisk
Asterisk

Reputation: 3574

Pagination in django mezzanine

I can't figure out how to use mezzanine pagination for my custom models.

The docs say:

mezzanine.core.templatetags.mezzanine_tags.pagination_for(parser, token) Include the pagination template and data for persisting querystring in pagination links. Can also contain a comma separated string of var names in the current querystring to exclude from the pagination links, via the exclude_vars arg.

As far as I understand in my template file I have to include mezzanine_tags and call {% pagination_for parser token %}.

I don't really understand what are parser and token. I looked at the source code of that template tag and it it as follows:

@register.inclusion_tag("includes/pagination.html", takes_context=True)
def pagination_for(context, current_page, page_var="page", exclude_vars=""):
    """
    Include the pagination template and data for persisting querystring
    in pagination links. Can also contain a comma separated string of
    var names in the current querystring to exclude from the pagination
    links, via the ``exclude_vars`` arg.
    """
    querystring = context["request"].GET.copy()
    exclude_vars = [v for v in exclude_vars.split(",") if v] + [page_var]
    for exclude_var in exclude_vars:
        if exclude_var in querystring:
            del querystring[exclude_var]
    querystring = querystring.urlencode()
    return {
        "current_page": current_page,
        "querystring": querystring,
        "page_var": page_var,
    }

Buy looking at usage I think that token is just number denoting current page. But how do I get context in the template?

Upvotes: 0

Views: 1464

Answers (1)

joshcartme
joshcartme

Reputation: 2747

Take a look at blog_post_list here: https://bitbucket.org/stephenmcd/mezzanine/src/902687d2753c449de31d4f615a3bf785ce914e96/mezzanine/blog/views.py?at=default#cl-16 and the associated template here https://bitbucket.org/stephenmcd/mezzanine/src/902687d2753c449de31d4f615a3bf785ce914e96/mezzanine/blog/templates/blog/blog_post_list.html?at=default

The proper usage is to use paginate in the view and then call {% pagination_for blog_posts %} where blog_posts is the return value of paginate. You don't need to worry about the parser, token, context etc... in the template tag, it is pulled in automatically.

Upvotes: 1

Related Questions