Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Calling specific values from a list that contains a dict using jinja2

I have a variable that contains a list that contains a dictionary. The dict contains variables that I would like to use in a script. For example var = [{pet:dog, activity:runs, owner:jerry, adjective:nice}]

The {{var.pet}} {{var.activity}}. The {{var.pet}}'s owner is {{var.owner}}. {{var.owner}} is very {{var.adjective}}.

I can't get this to work however. It just returns blank spaces where the variable should be.

Im trying to do this using jinja 2 in an html document.

Thanks in advance.

Can you show the python code that's rendering the template? You should have a call to the render() method I think where you're passing in your variables.

  {% for vars in aws_attr %}
    <p>Created by {{vars.Creator_Name}}</p>

    firewall_name
    <br>
    ae2.846 IP Address={{vars.OSPF_Subnet}}/29





    {% endfor %}
    <br>

The above seems to work, but Im concerned, because I will have several devices and configs and different HTML tags for each one and so forth. Im concerned that the above will cause unwanted repetition. I haven't had a chance to test this out beyond the one example I have there. As I said it seems to be working, so maybe this is the answer.

Here is the python code that generates the aws_attr and the page.

def sql_select(sql_var):
    connection = pymysql.connect(host="127.0.0.1",
                                 user="foo",
                                 passwd="foo",
                                 port=foo,
                                 database="directconnect")
    try:
        with connection.cursor(pymysql.cursors.DictCursor) as cursor:
            sql = """SELECT * FROM main WHERE Concat(idDirectConnect_ID,
                                             CR_Number,
                                             VPC_Name,
                                             Creator_Name,
                                             Route_Domain,
                                             OSPF_ASN,
                                             OSPF_VLAN,
                                             OSPF_Subnet,
                                             BGP_VLAN,
                                             BGP_Subnet,
                                             BGP_AuthKey,
                                             AWS_Expected_Subnet,
                                             AWS_Acnt,
                                             Company_Name,
                                             Lpbck_Int,
                                             Lpbck_IPAddr,
                                             Tunnel_Num,
                                             VPN_Int1_IPAddr,
                                             VPN_Int1_CryptoKey,
                                             VPN_Int1_Subnet,
                                             VPN_Int2_IPAddr,
                                             VPN_Int2_CryptoKey,
                                             VPN_Int2_Subnet,
                                             AWS_VPN_ConnectionID)
                                             LIKE %s;"""
            cursor.execute(sql, ('%{}%'.format(sql_var)))
            result = cursor.fetchall()
            return result
    finally:
        connection.close()


@app.route('/<name>', methods=['GET', 'POST'])
def awsdc_page(name):
    name = request.args.get('awsdc_name')
    return render_template('awsdc_page.html', aws_attr = sql_select(name))

1 Answer

Actually, it appears my code is working as I was hoping for. I was afraid that the {% for vars in aws_attr %} would cause it to repeat the following for each variable, which would be undesirable, but it is not doing that. I had a similar statement on another HTML page that was repeating and I didn't see the difference between the two, but I think I figure out the difference. In the one that was repeating there were multiple value for VPC_Name, but in the one I listed above, there is only one. There are other items, but there is only one of each item, so its only repeating once for each item.