Package: django-reverse-js
Django  Reverse JSJavaScript url handling for Django that doesn’t hurt.Original idea fromdjango-js-reverse.Django Reverse JSis a small django app that makes url handling ofnamed urlsin 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/'InstallationInstall usingpippipinstalldjango-reverse-js… or clone the project from github.gitclonehttps://github.com/sevdog/django-reverse-js.gitAdd'django_reverse_js'to yourINSTALLED_APPSsetting.INSTALLED_APPS=(...'django_js_reverse',)UsageAs a static fileFirst generate static file withcreatestatic_reversejscommanddjango-admincreatestatic_reversejsNOTE: 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<scriptsrc="{% static 'django_reverse_js/js/reverse.js' %}"></script>As viewInclude view in yourURLCONF(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<scriptsrc="{% url 'reverse_js' %}"type="text/javascript"></script>As template tagYou can place thereverse_jsJavaScript inline into your templates,
however use of inline JavaScript is not recommended, because it
may cause problems with Content Security Policy.
Seedjango-cspfor further readings.{%loadreversejs%}<script type="text/javascript" charset="utf-8">{%reverse_js%}</script>Use the resolver in JavaScriptIf your url names are validJavaScript identifiersyou can access them by thedot notation:Urls.betterlivingGetHouse('house',12)If the named url contains invalid identifiers use thesquare-bracket
notationinstead:NOTE: ATMnamespacedurlsmustbe accessd in this wayUrls['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})SettingsREVERSEJS_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 ofurl namespacesto be excluded from JavaScript resolver; default[](aka: all namespaces allowed).REVERSEJS_INCLUDE_ONLY_NAMESPACES: list ofurl namespacesto be included in JavaScript resolver; default[](aka: all namespaces allowed).use''(empty string) to allow only url without a namespaceuse'foo\0'(namespace name terminated withnull-char) to include only urls from'foo'namespace and prevent any inner namespace to be extracted (ie:'foo:bar')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 betweenREVERSEJS_INCLUDE_ONLY_NAMESPACESandREVERSEJS_EXCLUDE_NAMESPACESmay be used.