This function is a factory that validates received data and returns Django's Page object. In adition it also translates EmptyPage errors into Http404 errors and validates if first page number was explictly defined in url parameter or not.

paginate function has certain requirements on handling views that use it. Firstly, views with pagination should have two links instead of one:

previous - None if this is first page, otherwhise number of previous page.

next - None if this is last page, otherwhise number of next page.

last - None if this is last page, otherwhise number of the last page.

before - Total number of items on previous pages.

more - Total number of items left to display on next pages.

paginated_response(page, serializer=None, data=None, extra=None)

Shortcut function for returning paginated responses from API. Takes one required argument, the Page object, and following optional arguments:

serializer - Serializer to use. If its omited, no additional serialization step will be taken.

data - Data object to use. If its omited, page.object_list will be used by default.

extra - Dict with additional data to be included in response's JSON. Because dict in extra is added to response via response_json.update(extra), this dict can be used for last minute overrides as well.

Note

Giving page argument default value of 1 will make paginate function assume that first page was reached via link with explicit first page number and cause redirect loop.

Error handler expects link parameter that contains current page number to be named "page". Otherwise it will fail to create new link and raise KeyError.

validate_slug(model, slug)

This function compares model instance's "slug" attribute against user-friendly slug that was passed as link parameter. If model's slug attribute is different this function, misago.views.OutdatedSlug is raised. This exception is then captured by Misago's exception handler which makes Misago return permanent (http 301) redirect to client with valid link.

Example of view that first fetches object form database and then makes sure user or spider that reaches page has been let known of up-to-date link:

from misago.views.shortcuts import validate_slug, get_object_or_404

from myapp.models import Cake

​

defcake_fans(request, cake_id, cake_slug):

# first get cake model from DB

cake = get_object_or_404(Cake, pk=cake_id)

# issue redirect if cake slug is invalid

validate_slug(cake, cacke_slug)

Notes

You may have noticed that there's no exception handling for either Http404 exception raised by get_object_or_404, nor OutdatedSlug exception raised by validate_slug. This is by design. Both exceptions are handled by Misago for you so you don't have to spend time writing exception handling boiler plate on every view that fetches objects from database and validates their links.

Naturally if you need to, you can still handle them yourself.

Also, your links should use "slug" parameters only when they are supporting GET requests. For same reason you should call validate_slug only when request method is GET or HEAD.