### `url` ¶ Returns an absolute path reference (a URL without the domain name) matching a given view and optional parameters. Any special characters in the resulting path will be encoded using `iri_to_uri()`. This is a way to output links without violating the DRY principle by having to hard-code URLs in your templates: ``` {% url 'some-url-name' v1 v2 %} ``` The first argument is a URL pattern name. It can be a quoted literal or any other context variable. Additional arguments are optional and should be space-separated values that will be used as arguments in the URL. The example above shows passing positional arguments. Alternatively you may use keyword syntax: ``` {% url 'some-url-name' arg1=v1 arg2=v2 %} ``` Do not mix both positional and keyword syntax in a single call. All arguments required by the URLconf should be present. For example, suppose you have a view, `app_views.client`, whose URLconf takes a client ID (here, `client()` is a method inside the views file `app_views.py`). The URLconf line might look like this: ``` path("client/<int:id>/", app_views.client, name="app-views-client") ``` If this app’s URLconf is included into the project’s URLconf under a path such as this: ``` path("clients/", include("project_name.app_name.urls")) ``` …then, in a template, you can create a link to this view like this: ``` {% url 'app-views-client' client.id %} ``` The template tag will output the string `/clients/client/123/`. Note that if the URL you’re reversing doesn’t exist, you’ll get an `NoReverseMatch` exception raised, which will cause your site to display an error page. If you’d like to retrieve a URL without displaying it, you can use a slightly different call: ``` {% url 'some-url-name' arg arg2 as the_url %} <a href="{{ the_url }}">I'm linking to {{ the_url }}</a> ``` The scope of the variable created by the `as var` syntax is the `{% block %}` in which the `{% url %}` tag appears. This `{% url ... as var %}` syntax will _not_ cause an error if the view is missing. In practice you’ll use this to link to views that are optional: ``` {% url 'some-url-name' as the_url %} {% if the_url %} <a href="{{ the_url }}">Link to optional stuff</a> {% endif %} ``` If you’d like to retrieve a namespaced URL, specify the fully qualified name: ``` {% url 'myapp:view-name' %} ``` This will follow the normal namespaced URL resolution strategy, including using any hints provided by the context as to the current application. Warning Don’t forget to put quotes around the URL pattern `name`, otherwise the value will be interpreted as a context variable! ### `verbatim` ¶ Stops the template engine from rendering the contents of this block tag. A common use is to allow a JavaScript template layer that collides with Django’s syntax. For example: ``` {% verbatim %} {{if dying}}Still alive.{{/if}} {% endverbatim %} ``` You can also designate a specific closing tag, allowing the use of `{% endverbatim %}` as part of the unrendered contents: ``` {% verbatim myblock %} Avoid template rendering via the {% verbatim %}{% endverbatim %} block. {% endverbatim myblock %} ``` ### `widthratio` ¶ For creating bar charts and such, this tag calculates the ratio of a given value to a maximum value, and then applies that ratio to a constant. For example: ``` <img src="bar.png" alt="Bar" height="10" width="{% widthratio this_value max_value max_width %}"> ``` If `this_value` is 175, `max_value` is 200, and `max_width` is 100, the image in the above example will be 88 pixels wide (because 175/200 = .875; .875 \* 100 = 87.5 which is rounded up to 88). In some cases you might want to capture the result of `widthratio` in a variable. It can be useful, for instance, in a `blocktranslate` like this: ``` {% widthratio this_value max_value max_width as width %} {% blocktranslate %}The width is: {{ width }}{% endblocktranslate %} ``` ### `with` ¶ Caches a complex variable under a simpler name. This is useful when accessing an “expensive” method (e.g., one that hits the database) multiple times. For example: ``` {% with total=business.employees.count %} {{ total }} employee{{ total|pluralize }} {% endwith %} ``` The populated variable (in the example above, `total`) is only available between the `{% with %}` and `{% endwith %}` tags. You can assign more than one context variable: ``` {% with alpha=1 beta=2 %} ... {% endwith %} ``` Note The previous more verbose format is still supported: `{% with business.employees.count as total %}`