Django Reverse JS
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; defaultUrls. -
REVERSEJS_GLOBAL_OBJECT_NAME: global JavaScript object to which bound resolver variable; defaultwindow. -
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')
- use
-
REVERSEJS_SCRIPT_PREFIX: path of application (when served behing a reverse-proxy), needed to return full-urls; defaultNone. -
REVERSEJS_OUTPUT_PATH: path where to place file created bycreatestatic_reversejscommand, if not providedSTATIC_ROOTis used; defatulNone. -
REVERSEJS_MINIFY: flag which indicates if the minified version of JS script should be used; defaultFalse.
NOTE: at the moment only one between
REVERSEJS_INCLUDE_ONLY_NAMESPACESandREVERSEJS_EXCLUDE_NAMESPACESmay be used.
Metadata
Release files for django-reverse-js 0.1.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_reverse_js-0.1.9.tar.gz | 14.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_reverse_js-0.1.9-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 27.9 kB
Release files / django_reverse_js-0.1.9.tar.gz
| Download URL | django_reverse_js-0.1.9.tar.gz |
|---|---|
| Size | 14.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
841f26c2534a64c030bef53f255ac05e5135cd761db86418d37dc50937b7a71c
|
|
BLAKE2b-256 checksum How to use checksums |
cd4eb4f426fe7d3226a19368e8bb2ba10c0d881bf1190123d608c219e45017ac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Release files / django_reverse_js-0.1.9-py3-none-any.whl
| Download URL | django_reverse_js-0.1.9-py3-none-any.whl |
|---|---|
| Size | 13.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b2515fe1c3dc1c88c0f6ecb80dd10fc153f5e30b25088a3cb6f8c6cff4e40b56
|
|
BLAKE2b-256 checksum How to use checksums |
c142dc3f84cfc332f4e4217e828901c4df43cd82c9c78dc27b2905b45f433a96
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|