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 %} 
4
  • 2
    You can create a custom create filter (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". Commented Aug 6, 2019 at 10:52
  • What error message are you getting if any? Commented Aug 6, 2019 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. Commented Aug 6, 2019 at 10:55
  • 2
    Change your context in your view so that the policies dictionary has the relevant information about the associated claim. Commented Aug 6, 2019 at 10:59

1 Answer 1

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 %} 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.