Skip to main content

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


Download files

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

Source Distribution

django_urls_templatetag-0.9.1.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

django_urls_templatetag-0.9.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file django_urls_templatetag-0.9.1.tar.gz.

File metadata

File hashes

Hashes for django_urls_templatetag-0.9.1.tar.gz
Algorithm Hash digest
SHA256 4e2db3394f0d248076fd39266e785e0ffcc0ed6f0bce72f5654901017abf561f
MD5 875ea26c344318c97e993620fc1a4987
BLAKE2b-256 c68cdb806ccc4e79bc9e957f16c4067eac7756ba24763122f058a25279c9a62b

See more details on using hashes here.

File details

Details for the file django_urls_templatetag-0.9.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_urls_templatetag-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 81499c9d09ecc328686efd32f38f3c8accfaf63ce2200b0c75752db6d3515b17
MD5 91327d0ea93ad6b3fc5bc1aa7b4ee797
BLAKE2b-256 717a5b48b448b74d2b8296d26db02e452f25c1efd2f203f9f796bec331d1f55a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page