Skip to main content

django4-js-urls

License https://img.shields.io/pypi/pyversions/django4-js-urls.svg Latest Version

Django4-js-urls is a lightweight Django application allowing to easily get absolute path references matching given URL names and optional parameters on the client side - just like reverse does, but in a Javascript fashion.

Main requirements

Python 3.8+, Django 3.2+.

Installation

To install Django4-js-urls, please use pip (or pipenv) as follows:

$ pip install django4-js-urls

Once installed you just need to add js_urls to INSTALLED_APPS in your project’s settings module:

INSTALLED_APPS = (
    # other apps
    'js_urls',
)

URLs that are included in the Javascript helper tool are configured using a single JS_URL setting. This setting can embed URL namespaces or specific URL names that should be included in the generated Javascript file. For example:

JS_URLS = (
    'admin',
    'blog:article_list',
    'blog:article_detail',
)

Important: only URLs defined in this setting will be included in the generated Javascript file. You don’t want to expose all the URLs of your Django project in a Javascript file.

Then you can include the view responsible for generating the Javascript file in your URLs root module:

from js_urls.views import JsUrlsView

urlpatterns = [
    # other urls
    url('js-urls/$', JsUrlsView.as_view(), name='js_urls'),
]

And finally you can include the Javascript script in your base template as follows:

<script src="{% url 'js_urls' %}" type="text/javascript"></script>

Usage

URLs included in the generated Javascript file can be used in your scripts by using the window.reverse function. Here are some example:

const url1 = window.reverse('home');
const url2 = window.reverse('blog:article_list');
const url3 = window.reverse('blog:article_detail', articleId);
const url4 = window.reverse('blog:article_detail', [articleId, ]);
const url5 = window.reverse('blog:article_detail', { pk: articleId });

Settings

JS_URLS

Default: []

The JS_URLS setting allows to define URL namespaces or specific URL names that should be included in the generated Javascript file.

JS_URLS_FUNCTION_NAME

Default: 'reverse'

The JS_URLS_FUNCTION_NAME setting allows customize the name of the function used to reverse URLs on the client side. This function is made available through the window global object.

Authors

impak Finance <tech@impakfinance.com>.

License

MIT. See LICENSE for more details.

Metadata

Release files for django4-js-urls 0.1.3.3

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

Source distribution (sdist)

Source distribution for django4-js-urls 0.1.3.3
File Size Uploaded
django4-js-urls-0.1.3.3.tar.gz 7.7 kB Details

Release files / django4-js-urls-0.1.3.3.tar.gz

Download URL django4-js-urls-0.1.3.3.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
fc9e78cc7c25d5442c1b42b25d581406942d027c9626a912ed278386c867095d
BLAKE2b-256 checksum
How to use checksums
48b00822ae244e6766c511b517b400be621b1d8b21dd8bfffccf3cc1d9f1164f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.0

Release history Release notifications | RSS feed

This release

0.1.3.3 This release

1 release file

0.1.3.2

1 release file

0.1.3.1

1 release file

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