0

I need to fill my drop-down menu with some information that I have in my Database. Everything is working well, except the query result format.

This is the code in my python file:

    results = None
    with db:
        cur = db.cursor()
        sql = "SELECT banco FROM bancos"
        try:
            cur.execute(sql)
            results = cur.fetchall()
            return render_template("cadastro.html", results=results)

This is my HTML:

        <select name="teste">
        {% for r in results %}
            <option name="{{ r }}">{{ r }}</option>
        {% endfor %}
        </select>

It's is filling my drop-down menu with all information that i Want but in this format:

('Banco do ABC Brasil S.A.',)

I dont want the brackets, comma, etc. I just want the result!

How can I do that?

1 Answer 1

2

You are printing out the entire row, which in this case only contains one field, banco. To print just a specific field, you need to use that field in your template.

Try

    <select name="teste">
    {% for r in results %}
        <option name="{{ r[0] }}">{{ r[0] }}</option>
    {% endfor %}
    </select>

You may also be able to use r['banco'] or r.banco depending on your database driver.

Sign up to request clarification or add additional context in comments.

3 Comments

Tried this...returns nothing in the option field.
r should be a tuple. r[0] is the first element in the tuple.
Yes! I can't understand why it's returning nothing. Tried to use: return render_template("cadastro.html", results=results[0]) and It worked well returning the first record of the query formatted. well, i'll keep trying...thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.