Skip to main content

Django-js-routes is a Django application allowing to expose and perform reverse lookups of Django named URL patterns on the client side.

Main requirements

Python 3.6+, Django 3.2+.

Installation

To install Django-js-routes, please use the pip command as follows:

$ pip install django-js-routes

Once the package is installed, you’ll have to add the application to INSTALLED_APPS in your project’s settings module:

INSTALLED_APPS = (
    # all other apps...
    'js_routes',
)

You can then define which URL patterns or URL namespaces you want to expose to the client side by setting the JS_ROUTES_INCLUSION_LIST setting. This setting allows to define which URLs should be serialized and made available to the client side through the generated and / or exported Javascript helper. This list should contain only URL pattern names or namespaces. Here is an example:

JS_ROUTES_INCLUSION_LIST = [
    'home',
    'catalog:product_list',
    'catalog:product_detail',
]

Note that if a namespace is included in this list, all the underlying URLs will be made available to the client side through the generated Javascript helper. Django-js-routes is safe by design in the sense that only the URLs that you configure in this inclusion list will be publicly exposed on the client side.

Once the list of URLs to expose is configured, you can add the {% js_routes %} tag to your base template in order to ensure that the Javascript helper is available to you when you need it:

{% load js_routes_tags %}
<html>
    <head>
    </head>
    <body>
        <!-- At the bottom of the document's body... -->
        {% js_routes %}
    </body>
</html>

Usage

The URL patterns you configured through the JS_ROUTES_INCLUSION_LIST setting can then be reversed using the generated window.reverseUrl function, which can be used pretty much the “same” way you’d use reverse on the Django side:

window.reverseUrl('home');
window.reverseUrl('catalog:product_list');
window.reverseUrl('catalog:product_detail', productId);
window.reverseUrl('catalog:product_detail', { pk: productId });

Settings

JS_ROUTES_INCLUSION_LIST

Default: []

The JS_ROUTES_INCLUSION_LIST setting allows to define the URL patterns and URL namespaces that should be exposed to the client side through the generated Javascript helper.

Advanced features

Inserting only the serialized URLs in Django templates

By default, the {% js_routes %} template tag only allows to trigger the generation of the serialized URLs (which are stored in a Javascript object on the window object) and to include a Javascript URL resolver function in your HTML using the Django’s static template tag. Actually, a standard use of the {% js_routes %} statement is equivalent to:

{% js_routes routes_only=True %}
<script src="{% static 'js/routes/resolver.js' %}"></script>

The routes_only option allows to only include the serialized URLs in the output of {% js_routes %}. It gives you the ability to include the Javascript URL resolver that comes with Django-js-routes using another static statement. This also allows you to cache the output of the {% js_routes routes_only=True %} statement if you want (so that serialized URLs are not generated for every request).

Dumping the Javascript routes resolver

As explained earlier, the {% js_routes %} template tag triggers the generation of the serialized URLs and includes a client-side URL resolver in the final HTML. One downside of this behaviour is that the serialized URLs need to be generated every time your HTML template is rendered.

Instead it is possible to just dump the whole list of serialized URLs AND the URL resolver function into a single Javascript module file. This can be achieved using the dump_routes_resolver command, which can be used as follows:

$ python manage.py dump_routes_resolver --format=default --output=my_exported_resolver.js

The --output option allows to specify to which file the serialized routes and resolver function should be saved while the --format option allows to specify the Javascript format to use.

--format accepts the following values:

  • default includes the routes as an object that is associated to the window object while the URL resolver is available through the window.reverseUrl function (this corresponds to the behaviour provided by a standard use of the {% js_routes %} template tag)

  • es6 allows to save the routes and the URL resolver as an ES6 module where the reverseUrl function is the default export

License

MIT. See LICENSE for more details.

Metadata

Release files for django-js-routes 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-js-routes 0.2.0
File Size Uploaded
django-js-routes-0.2.0.tar.gz 11.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-js-routes 0.2.0
File Interpreter ABI Platform
django_js_routes-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 28.2 kB

Release files / django-js-routes-0.2.0.tar.gz

Download URL django-js-routes-0.2.0.tar.gz
Size 11.3 kB
Tags Source
SHA-256 checksum
How to use checksums
67f4fbd6d45cf0c0c5adae5e45e6de2aa4a7e2505e073cb5550ce2503ea986aa
BLAKE2b-256 checksum
How to use checksums
acdefbf600fb54c2ee89fc87a8dd731a28b9e14e406ff54161b57aa77a575a00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.10.1 Darwin/21.1.0

Release files / django_js_routes-0.2.0-py3-none-any.whl

Download URL django_js_routes-0.2.0-py3-none-any.whl
Size 16.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9adc2f5e1874d238ec3004dbde3cd0220bcef56669a607847502f1f34c2ddccd
BLAKE2b-256 checksum
How to use checksums
d35b6bf7cfd2325581c3ad9cc91efc85ab020aacd4a83a4d43f35e2d4d3c326f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.10.1 Darwin/21.1.0

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page