Reverse django URLs within JavaScript.
Project description
Description
django-urls-templatetag
allows you to reverse django URLs within JavaScript exactly as you do from within Python using the standard reverse
function.
Installation and usage
From your django virtual environment, run pip install django-urls-templatetag
, add django_urls_templatetag
inside your INSTALLED_APPS
, then, in your base.html
template (or any template which defines your JavaScript includes in the HTML <head>
tag), add {% load django_urls %}
and then include {% django_urls_script 'url_namespace1' 'url_namespace2' ... %}
somewhere inside the <head>
tag of your HTML template, above any JavaScript library or code which will make use of this. When you include the django_urls_script
template tag, you will need to specify which namespaces you want reversed. This is not an automatic process because that could introduce security vulnerabilities by exposing all available URL endpoints. With the template tag included, you can now use a reverse_url
function within JavaScript.
Example
Say we have a project which has URLs under an "api" namespace and URLs under a "blog" namespace. In django, we would reverse these with reverse("api:blog_post_list")
or reverse("blog:index")
for example. To get the same effect in JavaScript, I would include {% django_urls_script 'api' 'blog' %}
inside my <head>
tag in my template. Then, to reverse the URLs from inside JavaScripr, I would do reverse_url("api:blog_post_list")
or reverse_url("blog:index")
. Both of these would return as a string the URLs of either endpoint.
Reversing URLs with keyword arguments is also possible. Simply provide the keyword arguments as a JavaScript object as a second argument. An example might look like this: reverse_url("blog:blog_post_detail", {pk: 55})
. Easy and simple.
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
File details
Details for the file django_urls_templatetag-0.9.1.tar.gz
.
File metadata
- Download URL: django_urls_templatetag-0.9.1.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e2db3394f0d248076fd39266e785e0ffcc0ed6f0bce72f5654901017abf561f |
|
MD5 | 875ea26c344318c97e993620fc1a4987 |
|
BLAKE2b-256 | c68cdb806ccc4e79bc9e957f16c4067eac7756ba24763122f058a25279c9a62b |
File details
Details for the file django_urls_templatetag-0.9.1-py3-none-any.whl
.
File metadata
- Download URL: django_urls_templatetag-0.9.1-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81499c9d09ecc328686efd32f38f3c8accfaf63ce2200b0c75752db6d3515b17 |
|
MD5 | 91327d0ea93ad6b3fc5bc1aa7b4ee797 |
|
BLAKE2b-256 | 717a5b48b448b74d2b8296d26db02e452f25c1efd2f203f9f796bec331d1f55a |