A convenient helper to use Django URLs in Javascript.
Project description
Django-js-url 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.4+, Django 1.11+.
Installation
To install Django-js-urls, please use pip (or pipenv) as follows:
$ pip install django-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(r'^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 });
License
MIT. See LICENSE for more details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_js_urls-0.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ced82646919f25d9c6cd4b5c189998d9b5483f1567bcd51cf5776e8961fbf5b |
|
MD5 | 3838a2fd8dbcec7b2fac43fcd655e45e |
|
BLAKE2b-256 | 90a2e6d4fe633461576c220b40da03b07e0801c7c4ce05c6d3f129d8205b748b |