I have Variable :
unit = 'BARS'
i pass this value into frontend using Django template
I want to get 'bar'
I use {{ unit|slice":4"|lower }}
but it doesn't work .
Add filter like this
{{ unit|lower|slice:'0:3' }}
For more information open link
https://www.djangotemplatetagsandfilters.com/filters/slice/
Related
I'm trying to access a property with a $ in its name in a django template. Unfortunately I have no control over filters nor over the variable names.
The object is structured as follows:
{
"title": "Some title",
"metadata": {
"$price": 9.99,
"$inventory_policy": 1
}
}
I am trying to access {{ item.metatadata.$price }}, but the template builder crashes with an unspecified error.
I already tried the workarounds for python templates, but they crash as well:
{{ item.metatadata.$$price }}
{{ item.metatadata.${price} }}
For future reference, this is in a Klaviyo template.
I think you can use a custom filter for doing that : https://docs.djangoproject.com/fr/4.1/howto/custom-template-tags/
in templatetags.extras.py
from django import template
register = template.Library()
#register.filter
def get_value(dictio, key):
return dictio.get(key, '')
and for using it:
{% load extras %}
{{ item.metatadata|get_value:"$price" }}
Nevermind. They supply an undocumented lookup filter for exactly that reason. It probably looks exactly like in Lucas Grugru's answer.
{{ item.metatadata|lookup:"$price" }}
The relevant section in the Klaviyo documentation is https://help.klaviyo.com/hc/en-us/articles/115005084927-Guide-to-Template-Tags-and-Variable-Syntax#event-variables-4, where the lookup filter is used in an unrelated example.
Have a flask wtforms select field and trying to incorporate htmx ajax call, which has dashes in data attributes so I found a solution on SO like the following:
{{ form.dia(class="form-select", **{'hx-post': "/pulleys/{{pulley_id}}/hub_type", 'hx-target': "#hub-shaft-selection", 'hx-swap': "innerHTML"}) }}
but the {{pulley_id}} doesn't get parsed by the template, I guess it's already inside another {{ }}.
Is there a way to build the dynamic part as shown above so it ends up as
'hx-post'="/pulleys/1/hub_type"
when fully rendered for pulley_id=1
If pulley_id is a variable either inside a loop or passed into render_template your should be able to format the string:
{{ form.dia(class_="form-select", **{'hx-post': "/pulleys/%s/hub_type"|format(pulley_id), 'hx-target': "#hub-shaft-selection", 'hx-swap': "innerHTML"}) }}
Note: you also want class_ not class if you're trying to set the HTML class attribute.
Can I use the path() function in twig to create a link to an entity add form. Taking the group module as an example, if I want to create a link to the 'group/add' page in twig can I use something like:
{{ path(entity.group.add) }}
I know that the above does not work, neither does
{{ path(group.add) }}
Not sure if there is a way to do this with the various twig functions that create links.
According to the first action in the group.links.action.yml file, you should use this:
{{ path('entity.group.add_page') }}
Is there any way to get the id of a field in a template?
In the HTML I get: <input name="field_name" id="id_field_name"...
I know I can get the name with {{ field.html_name }}, but is there anything similar for getting the id?
Or can I only get it like this: id_{{ field.html_name }}?
You can get the ID like this:
{{ field.auto_id }}
You can also use id_for_label:
{{ field.id_for_label }}
This doesn't work for every form field.
For instance {{ form.address.auto_id }} works while {{ form.address.auto_name }} will not.
However you can use {{ form.address.html_name }} to get the equivalent answer.
Here are the docs
From the documentation-
each form field has an ID attribute set to id_<field-name>
, which is referenced by the accompanying label tag. This is important in ensuring that forms are accessible to assistive technology such as screen reader software. You can also customize the way in which labels and ids are generated.
So I would like to say id_field-name , you collect the field name from the model.
Here is the link to the documentation
In Django 2 you can retrieve the ID for a specific field using {{ field.id_for_label }}
This is documented here.
Pretty simple. I have a Python list that I am passing to a Django template.
I can specifically access the first item in this list using
{{ thelist|first }}
However, I also want to access a property of that item... ideally you'd think it would look like this:
{{ thelist|first.propertyName }}
But alas, it does not.
Is there any template solution to this, or am I just going to find myself passing an extra template variable...
You can access any item in a list via its index number. In a template this works the same as any other property lookup:
{{ thelist.0.propertyName }}
You can combine the with template tag with the first template filter to access the property.
{% with thelist|first as first_object %}
{{ first_object.propertyname }}
{% endwith %}
If you're trying to access a manytomany field, remember to add all, so it will look like object.m2m_field.all.0.item_property
a potentially clearer answer/syntax for accessing a ManyToManyField property in an object list provided to the django template would look like this:
{{ object_list.0.m2m_fieldname.all.0.item_property }}