The Django template traverses the value of the b dictionary with the key, of the a dictionary

        "sku": {
            "20509": ["28314", "28315", "28316", "28317", "6145171"],
            "1627207": ["28324", "28326", "28341", "28320"]
        },
        "sku_map": {
            "20509": "",
            "28314": "S",
            "28315": "M",
            "28316": "L",
            "28317": "XL",
            "6145171": "2XL",
            "1627207": "",
            "28324": "",
            "28326": "",
            "28341": "",
            "28320": ""
        },

I want to use the key value of the sku dictionary to display the sku_map values on the template. Can the template syntax of Django be implemented? Or do you need to pass it on view?

Mar.04,2021

  1. Custom filter
from django import template

register = template.Library()


@register.filter()
def hash(h, key):
    if key in h:
        return h[key]
    else:
        return None
  1. template calls
{{ sku_map|hash:key }}

reference: https://stackoverflow.com/que.

https://docs.djangoproject.co.

Menu