django-js-reverse

raw JSON →
0.10.2 verified Fri May 01 auth: no python maintenance

A Django app to generate JavaScript code that provides URL reversing similar to Django's reverse() function. Version 0.10.2. Release cadence is low; latest release 0.10.2 in 2022.

pip install django-js-reverse
error No module named 'js_reverse'
cause Importing from 'js_reverse' instead of 'django_js_reverse'
fix
Use 'django_js_reverse' everywhere: INSTALLED_APPS, imports, etc.
error Reverse for '...' not found. '...' is not a valid view function or pattern name.
cause The JavaScript URL generation is called before the URL configuration is fully loaded (e.g., in a script tag in <head>).
fix
Move the inline script to the end of <body> or use window.onload.
gotcha The middleware must be placed after `django.middleware.common.CommonMiddleware` in MIDDLEWARE setting.
fix Ensure MIDDLEWARE ordering: CommonMiddleware before JsReverseMiddleware.
deprecated The `JS_REVERSE_EXCLUDE_NAMESPACES` setting is deprecated in favor of `JS_REVERSE_EXCLUDE_URLS`.
fix Use `JS_REVERSE_EXCLUDE_URLS` instead.

After installation and configuration, use the template tag to inline the JS reverse code.

# settings.py
INSTALLED_APPS = [
    ...
    'django_js_reverse',
]

MIDDLEWARE = [
    ...
    'django_js_reverse.middleware.JsReverseMiddleware',
]

# template
{% load js_reverse %}
<script>{% inline_js_reverse %}</script>