placeholders¶
The urls_to_js tag avoids error prone string processing by using Django’s
django.urls.reverse() mechanism to generate the URLs to embed in the javascript.
To do the reversal it needs temporary placeholder values to feed in as kwargs or args.
There aren’t reliable or license friendly libraries available to generate the
placeholders directly from the regular expressions so users are relied upon to
supply them. This module provides facilities for registering and resolving
those placeholders. It also contains some predefined placeholders for the admin
module.
Users are strongly encouraged to use paths instead of re_paths and to supply custom converters when needed to avoid the need for re_paths. This should make the placeholder registration process as painless as possible. All of the builtin converters already have placeholders registered for them. Custom converters can simply add a placeholder class attribute that will be used without requiring an explicit registration.
Note
Many placeholders may be registered for a variable name/app_name. They’ll be tried until one is found to work, and prioritized in the order of most specific registration. This means a placeholder registered against app1 and variable name var1 will be tried before the placeholder registered against var1. Converters are the most specific registration info.
- render_static.placeholders.register_converter_placeholder(converter_type, placeholder)[source]¶
Register a placeholder for the given converter type. This registry function is intended to allow placeholders to be registered for converters outside the control of the calling code base. For converters under your control you should add a placeholder attribute to the converter class instead.
- render_static.placeholders.register_variable_placeholder(var_name, placeholder, app_name=None)[source]¶
Register a placeholder for a specific variable name and also optionally an app_name.
- render_static.placeholders.register_unnamed_placeholders(url_name, placeholders, app_name=None)[source]¶
Register a list of placeholders for a url_name and optionally an app_name that takes unnamed arguments. The list indices should correspond to the argument order as passed to reverse.
- render_static.placeholders.resolve_placeholders(var_name, app_name=None, converter=None)[source]¶
Resolve placeholders for named variables that match the given lookup parameters.