Skip to main content

Django Reverse JS

Tests PyPI pyversions Django version

JavaScript url handling for Django that doesn’t hurt.

Original idea from django-js-reverse.

Django Reverse JS is a small django app that makes url handling of named urls in JavaScript easy and confortable for django's users.

For example you can retrieve a named url:

path('/betterliving/<str:category_slug>/<int:entry_pk>/', 'get_house', name='betterliving_get_house')

in JavaScript it can be resolved like:

Urls.betterlivingGetHouse('house', 12)
// '/betterliving/house/12/'

Installation

Install using pip

pip install django-reverse-js

… or clone the project from github.

git clone https://github.com/sevdog/django-reverse-js.git

Add 'django_reverse_js' to your INSTALLED_APPS setting.

INSTALLED_APPS = (
    ...
    'django_reverse_js',
)

Usage

As a static file

First generate static file with createstatic_reversejs command

django-admin createstatic_reversejs

NOTE: If you change some urls or add an app and want to update the reverse.js file by running again the command.

After this add the file to your template

<script src="{% static 'django_reverse_js/js/reverse.js' %}"></script>

As view

Include view in your URLCONF (you may also cache this is needed):

urlpatterns = [
    ...,
    path('reverse.js', 'django_reverse_js.views.urls_js', name='reverse_js'),
]

Then include JavaScript in your template

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

As template tag

You can place the reverse_js JavaScript inline into your templates, however use of inline JavaScript is not recommended, because it may cause problems with Content Security Policy. See django-csp for further readings.

{% load reversejs %}

<script type="text/javascript" charset="utf-8">
    {% reverse_js %}
</script>

Use the resolver in JavaScript

If your url names are valid JavaScript identifiers you can access them by the dot notation:

Urls.betterlivingGetHouse('house', 12)

If the named url contains invalid identifiers use the square-bracket notation instead:

NOTE: ATM namespaced urls must be accessd in this way

Urls['betterliving-get-house']('house', 12)
Urls['namespace:betterliving-get-house']('house', 12)

You can also pass javascript objects to match keyword aguments like the examples bellow:

Urls['betterliving-get-house']({ category_slug: 'house', entry_pk: 12 })
Urls['namespace:betterliving-get-house']({ category_slug: 'house', entry_pk: 12 })

Settings

  • REVERSEJS_VAR_NAME: name given to JavaScript variable used to access django urls; default Urls.

  • REVERSEJS_GLOBAL_OBJECT_NAME: global JavaScript object to which bound resolver variable; default window.

  • REVERSEJS_EXCLUDE_NAMESPACES: list of url namespaces to be excluded from JavaScript resolver; default [] (aka: all namespaces allowed).

  • REVERSEJS_INCLUDE_ONLY_NAMESPACES: list of url namespaces to be included in JavaScript resolver; default [] (aka: all namespaces allowed).

    • use '' (empty string) to allow only url without a namespace
    • use 'foo\0' (namespace name terminated with null-char) to include only urls from 'foo' namespace and prevent any inner namespace to be extracted (ie: 'foo:bar')
  • REVERSEJS_SCRIPT_PREFIX: path of application (when served behing a reverse-proxy), needed to return full-urls; default None.

  • REVERSEJS_OUTPUT_PATH: path where to place file created by createstatic_reversejs command, if not provided STATIC_ROOT is used; defatul None.

  • REVERSEJS_MINIFY: flag which indicates if the minified version of JS script should be used; default False.

NOTE: at the moment only one between REVERSEJS_INCLUDE_ONLY_NAMESPACES and REVERSEJS_EXCLUDE_NAMESPACES may be used.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_reverse_js-0.1.8.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_reverse_js-0.1.8-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file django_reverse_js-0.1.8.tar.gz.

File metadata

  • Download URL: django_reverse_js-0.1.8.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for django_reverse_js-0.1.8.tar.gz
Algorithm Hash digest
SHA256 42739b2d955704cb723467655c67278602695d2aae6e7595ff80db323c73e958
MD5 66ebe6b439dcca994bd2edf9b891271d
BLAKE2b-256 a4c36ec7808f31a53913928ecdcb580efa88c78ee7394e20f95bbde465630719

See more details on using hashes here.

File details

Details for the file django_reverse_js-0.1.8-py3-none-any.whl.

File metadata

File hashes

Hashes for django_reverse_js-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 e9e604aaf8c5cda7c1b1cb9a1a78cff395f7937935085d22bcd484016512a069
MD5 2292ed835f1ca46fdcd21671a5f690c6
BLAKE2b-256 6657a9f08a4e5ad1e8c4052e7ad53b4243680c7a43f0bc6cf0d0bf8fbecb322f

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.8 This release

2 files

0.1.7

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page