Skip to content
This repository has been archived by the owner on Mar 15, 2018. It is now read-only.

Guide to URLs

Matt Basta edited this page Nov 8, 2013 · 1 revision

There are many built-in utilities in Commonplace for constructing and interpreting URLs.

The urls module

urls.reverse

urls.reverse(view_name[, arguments])

The reverse() method allows you to generate an absolute URL to a view within your project. The first parameter should be the view name (as defined by the view_name field in the routes.js file). If the route accepts arguments, they should be passed to the arguments parameter. arguments must be a list of strings, if specified.

This method will always return a string.

urls.api.url

urls.api.url(api_route[, arguments[, parameters]])

The api.url() method generates a URL based on API routes. api_route is the name of the route (as defined in routes_api.js; see the guide to API routes for more). arguments is a list of strings which will be formated into the URL. parameters may also be specified, which adds URL parameters to the resulting URL. Note: parameters will be overridden by default API parameters (such as lang, region, etc.).

utils.urlparams(urls.api.url('route', ['foo']), {abc: 'def'});
// equivalent to
urls.api.url('route', ['foo'], {abc: 'def'});

urls.api.params

urls.api.params(api_route, parameters)

Shorthand for urls.api.url(api_route, [], parameters).

urls.api.sign

urls.api.sign(url)

This method will add the default API parameters to url.

urls.api.unsigned

This is a namespace that provides methods that do not add default API parameters. This is the same as the urls.api.* variants, sans the call to urls.api.sign.

urls.api.unsigned.url(api_route[, arguments[, parameters]])
urls.api.unsigned.params(api_route, parameters)

urls.media

urls.media(path)

This method returns a URL to a media resource. The path that media files can be found should be specified in settings.media_url. This will automatically figure out whether to append or prepend slashes.