4

I want to populate the data that Im getting from Webservice - JSON response in Template.

My Service integration code:

serviceRequest  = requests.get(ServiceSettings.getCitiesURL(),  
headers={
"Content-Type":servicesettings.JSON_CONTENT_TYPE,
"Accept":servicesettings.HEADER_ACCEPT 
})                                  
dataJson = serviceRequest.json ()

Response that Im getting is

{"cities": [{"latitude": "21.321", "cityIdentifier": "GOOD", "cityName": "NY", "longitude": "23.23432"} , {"latitude": "1.321", "cityIdentifier": "GOOD", "cityName": "CA", "longitude": "3.23432"}
], "statusMessage": "OK", "statusCode": 200}

I'm trying to iterate it in DJango Tempalte (HTML) like below but am not not able to list the cityName

{% for objCities in cityList%} 
    {% for objCity in objCities.citiess%} 
        {{objCity.cityName}}
    {% endfor%}
{% endfor%}
1
  • You're not parsing it in your template: requests is doing the parsing, in your view. In your template, you're iterating. Commented Nov 30, 2013 at 9:35

1 Answer 1

6

In your loop, you were trying to access a string with an index. So TypeError: string indices must be integers, not str was raised.

Consider following:

{% for objCity in cityList.cities %} 
    {{objCity.cityName}}
{% endfor%}

This will work.

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.