1๐
โ
You can make a custom template filter.
Make a custom_filters.py
file inside a directory called templatetags
inside one of your app directories. For example, if you have an app called myapp
, then you should have a directory called myapp/templatetags/
containing the custom_filters.py
file.
Then in the custom_filters.py
file write the following:
from django import template
register = template.Library()
@register.filter
def get_item(dictionary, key):
return dictionary.get(key)
Then in the template, you can use the filter like this:
{% load custom_filters %}
{% for package in packages %}
package name: {{ package }}
{% with forloop.counter0 as index %}
{{ package_main_prices_list|get_item:index }}
{% endwith %}
date: {{ some_date }}
{% endfor %}
Load the custom filters at the top of the file.
Make sure to restart the server after making changes.
๐คSunderam Dubey
- [Answered ]-How do I use a virtualenv-based deployment method without upgrading to each and every upstream version?
- [Answered ]-Can variables with lookups be used in Django {% ifequal %} expressions?
- [Answered ]-How to add a column for displaying small size of img inside backgrid.
- [Answered ]-How to include my base template with custom context in Django comment templates?
- [Answered ]-Understanding how to create django Models
Source:stackexchange.com