Python dict values

I’ve been trying out django for a project, I’m just starting to get the first results in my very first app.

I’m looping though API call responses, looking for specific keys and values. If certain values are found, other API calls are made. This all works fine. To provide variables to the front end I have created a simple dictonary, add values to that as needed and then include it with template.render():

    # Snippet from views.py

    devices = []
    for result in response['results']:
        dev = {
            'v1': result['v1'],
            'v2': result['v2'] if 'v2' in result.keys() else '',
            'v3': result['v3'] if 'v3' in result.keys() else '',
        }

        devices.append(dev)

    context = {
        'response': devices,
        'search_string': query
    }

    return HttpResponse(template.render(context))

In the HTML file I present the data in a table:

<!-- Snippet from index.html -->

{% for x in response %}
<tr>
      <td>{{ x.v1 }}</td>
      <td>{{ x.v2 }}</td>
      <td>{{ x.v3 }}</td>
</tr>
{% endfor %}

However, depending on the result from the previous API call, I may want to add information to the dictionary:

# views.py

if (condition):
    dev['v4'] = 'v4',
    dev['v5'] = 'v5',

Here comes the issue: When I add v4 and v5 like above, they do not appear as simple string values in the HTML file. They instead appear as some sort of list, it looks like a python Tuple:

{   'v1': 'v1',
    'v2': 'v2',
    'v3': 'v3',
    'v4': ('v4',),
    'v5': ('v5',)
}

If I print() the values, python simply puts out a string, and type() also return a string type. I can do another loop in the HTML file, but there must be a better solution.

What do I do wrong?

I think we’re going to need to see the full code here as it relates to v4 and v5 - how they get added to dev in the view and how they’re being rendered in the template. We may also need to see what the real data is that you’re getting in “response” to understand how it’s seen by Python.