1

My Django app is interacting with an API and displaying results in the templates.

The API result has some normal Key Value pairs and some custom fields which have a Key in double and single quotations.

The key is formatted as " 'custom_field_123' " in the result JSON:

{'cost_price': '0.00', "'asset_field_1234'": None, "'asset_field_5768'": None}

I know this isn't correct JSON format but it's what I have to deal with.

In the Django templates I can use:

<p>{{tower.name}}</p>

for regular format keys. But the template language will not recognize:

<p>{{tower.'custom_field_123'}}</p>

Or

<p>{{tower.('custom_field_123')}}</p>

Is there a way to access these values or will I have to rename the keys in quotations to access them in the template.

Bchadwick
  • 347
  • 1
  • 5
  • 15

1 Answers1

2

From the template reference docs:

Variable names must consist of any letter (A-Z), any digit (0-9), an underscore (but they must not start with an underscore) or a dot.

So quotes aren't allowed, and the template language has no way to directly access those properties.

You could write your own template tag to look up dictionary items by a variable key (as in this question) but I would suggest fixing your data.

Community
  • 1
  • 1
RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79
  • Yeah suspected this would be the answer, Unfortunately the data is coming from another service, Thanks. – Bchadwick Jan 30 '17 at 13:04