0

I have two dict in my view function

policy = {{'policy_no':'123'},{'policy':'456'}}

claim = {{'123':'ACTIVE'}}

In my template file I dont want to iterate 'claim' dict.

view.py


policies = {{'policy_no':'123'},{'policy':'456'}}

claims = {{'123':'ACTIVE'}}

template file

{% for policy in policies %}
   {{claims[policy.policy_no]}} # I want to access directly 'ACTIVE'
{% endfor %}
Sumit Nayak
  • 307
  • 3
  • 13
  • 2
    You can create a custom create filter (https://stackoverflow.com/q/50020473/67579) but it is *not* recommended to do such things in a template. A template should be used for "rendering" logic, not "business logic". – Willem Van Onsem Aug 06 '19 at 10:52
  • What error message are you getting if any? – Daniel Butler Aug 06 '19 at 10:53
  • 2
    @DanielButler: Django templates do not support full Python syntax *deliberately*: function calls and subscripts can not be done. One can use Jinja, but usually it means something is not entirely done correctly in the template. – Willem Van Onsem Aug 06 '19 at 10:55
  • 2
    Change your context in your view so that the `policies` dictionary has the relevant information about the associated `claim`. – dirkgroten Aug 06 '19 at 10:59

1 Answers1

0

I used custom templatetags filter and solved the problem. Thanks to @ Willem Van Onsem

core_extras.py

from django.template.defaulttags import register

@register.filter
def get_item(dictionary, key):
    return dictionary.get(key)

template file:

{% load core_extras %}
{% for policy in policies %}
   {{ claims|get_item:policy.policy_no }}
{% endfor %}
Sumit Nayak
  • 307
  • 3
  • 13