All Projects → ellmetha → django-js-routes

ellmetha / django-js-routes

Licence: MIT license
Expose and perform reverse lookups of Django URLs in the frontend world.

Programming Languages

python
139335 projects - #7 most used programming language
javascript
184084 projects - #8 most used programming language
Makefile
30231 projects
HTML
75241 projects
SCSS
7915 projects

Projects that are alternatives of or similar to django-js-routes

urltree
Named URL data structure with support for URL building.
Stars: ✭ 36 (+80%)
Mutual labels:  routes, urls
DiscordChatExporterPy
A simple Discord chat exporter for Python Discord bots.
Stars: ✭ 72 (+260%)
Mutual labels:  export
evernote-backup
Backup & export all Evernote notes and notebooks
Stars: ✭ 104 (+420%)
Mutual labels:  export
vue-laroute
Wrapper for injecting laravel routes into your vue application
Stars: ✭ 23 (+15%)
Mutual labels:  routes
ESRGAN-tensorflow
Enhanced SRGAN. Champion PIRM Challenge on Perceptual Super-Resolution
Stars: ✭ 33 (+65%)
Mutual labels:  resolution
signal-media-exporter
A script to export media files from Signal Desktop.
Stars: ✭ 19 (-5%)
Mutual labels:  export
spreadsheet
Yii2 extension for export to Excel
Stars: ✭ 79 (+295%)
Mutual labels:  export
PackageProject.cmake
🏛️ Help other developers use your project. A CMake script for packaging C/C++ projects for simple project installation while employing best-practices for maximum compatibility.
Stars: ✭ 48 (+140%)
Mutual labels:  export
CTF
CTF binary exploit code
Stars: ✭ 37 (+85%)
Mutual labels:  reverse
kcl-timetable
Utilities in Python3 to fetch the KCL Timetable for a user, or export it as iCalendar (*.ics for Google or Apple)!
Stars: ✭ 14 (-30%)
Mutual labels:  export
porter
Export legacy forums into a format Vanilla Forums can import.
Stars: ✭ 39 (+95%)
Mutual labels:  export
ckan-php-manager
A tool for managing a CKAN data catalog
Stars: ✭ 14 (-30%)
Mutual labels:  export
excel mysql
Module for import Excel files to MySQL table and export MySQL table to Excel file using PHPExcel
Stars: ✭ 30 (+50%)
Mutual labels:  export
browserexport
backup and parse browser history databases (chrome, firefox, safari, and other chrome/firefox derivatives)
Stars: ✭ 54 (+170%)
Mutual labels:  export
wdumper
Tool for generating filtered Wikidata RDF exports
Stars: ✭ 25 (+25%)
Mutual labels:  export
elastic-query-export
🚚 Export Data from ElasticSearch to CSV/JSON using a Lucene Query (e.g. from Kibana) or a raw JSON Query string
Stars: ✭ 56 (+180%)
Mutual labels:  export
resolution-go
Golang library for resolving unstoppable domains
Stars: ✭ 24 (+20%)
Mutual labels:  resolution
Psychic-CCTV
A video analysis tool built completely in python.
Stars: ✭ 21 (+5%)
Mutual labels:  resolution
res
Device resolution detection module
Stars: ✭ 35 (+75%)
Mutual labels:  resolution
resolution
A library to resolve blockchain domain names.
Stars: ✭ 190 (+850%)
Mutual labels:  resolution

django-js-routes

License Latest Version Build status Codecov status

Django-js-routes is a Django application allowing to expose and perform reverse lookups of Django named URL patterns on the client side.

Main requirements

Python 3.6+, Django 3.2+.

Installation

To install Django-js-routes, please use the pip command as follows:

$ pip install django-js-routes

Once the package is installed, you'll have to add the application to INSTALLED_APPS in your project's settings module:

INSTALLED_APPS = (
    # all other apps...
    'js_routes',
)

You can then define which URL patterns or URL namespaces you want to expose to the client side by setting the JS_ROUTES_INCLUSION_LIST setting. This setting allows to define which URLs should be serialized and made available to the client side through the generated and / or exported Javascript helper. This list should contain only URL pattern names or namespaces. Here is an example:

JS_ROUTES_INCLUSION_LIST = [
    'home',
    'catalog:product_list',
    'catalog:product_detail',
]

Note that if a namespace is included in this list, all the underlying URLs will be made available to the client side through the generated Javascript helper. Django-js-routes is safe by design in the sense that only the URLs that you configure in this inclusion list will be publicly exposed on the client side.

Once the list of URLs to expose is configured, you can add the {% js_routes %} tag to your base template in order to ensure that the Javascript helper is available to you when you need it:

{% load js_routes_tags %}
<html>
    <head>
    </head>
    <body>
        <!-- At the bottom of the document's body... -->
        {% js_routes %}
    </body>
</html>

Usage

The URL patterns you configured through the JS_ROUTES_INCLUSION_LIST setting can then be reversed using the generated window.reverseUrl function, which can be used pretty much the "same" way you'd use reverse on the Django side:

window.reverseUrl('home');
window.reverseUrl('catalog:product_list');
window.reverseUrl('catalog:product_detail', productId);
window.reverseUrl('catalog:product_detail', { pk: productId });

Settings

JS_ROUTES_INCLUSION_LIST

Default: []

The JS_ROUTES_INCLUSION_LIST setting allows to define the URL patterns and URL namespaces that should be exposed to the client side through the generated Javascript helper.

Advanced features

Inserting only the serialized URLs in Django templates

By default, the {% js_routes %} template tag only allows to trigger the generation of the serialized URLs (which are stored in a Javascript object on the window object) and to include a Javascript URL resolver function in your HTML using the Django's static template tag. Actually, a standard use of the {% js_routes %} statement is equivalent to:

{% js_routes routes_only=True %}
<script src="{% static 'js/routes/resolver.js' %}"></script>

The routes_only option allows to only include the serialized URLs in the output of {% js_routes %}. It gives you the ability to include the Javascript URL resolver that comes with Django-js-routes using another static statement. This also allows you to cache the output of the {% js_routes routes_only=True %} statement if you want (so that serialized URLs are not generated for every request).

Dumping the Javascript routes resolver

As explained earlier, the {% js_routes %} template tag triggers the generation of the serialized URLs and includes a client-side URL resolver in the final HTML. One downside of this behaviour is that the serialized URLs need to be generated every time your HTML template is rendered.

Instead it is possible to just dump the whole list of serialized URLs AND the URL resolver function into a single Javascript module file. This can be achieved using the dump_routes_resolver command, which can be used as follows:

$ python manage.py dump_routes_resolver --format=default --output=my_exported_resolver.js

The --output option allows to specify to which file the serialized routes and resolver function should be saved while the --format option allows to specify the Javascript format to use.

--format accepts the following values:

  • default includes the routes as an object that is associated to the window object while the URL resolver is available through the window.reverseUrl function (this corresponds to the behaviour provided by a standard use of the {% js_routes %} template tag)
  • es6 allows to save the routes and the URL resolver as an ES6 module where the reverseUrl function is the default export

License

MIT. See LICENSE for more details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].