Using Jinja to loop through a DB table getting error: ‘DB name’ is undefined

Been working on creating a web app using flask. Was following this tutorial: https://www.youtube.com/watch?v=dam0GPOAvVI&t=5286s

My app is a banking app unlike in the video. At this point in my project I am past the video and am trying to create a page where users can view bank accounts that were created by them after they have been stored in the DB.

Using Jinja I tried a for loop to get the accounts associated to a specific user. In the video the guys uses a very similar for loop

{% block content %} <div class="container py-3 p-3 my-3 border">
<h3 style="text-align:Center">Your Accounts</h3>
<form name="banks">
    {% for bank in account.bank_accounts %}
    <label for="banks">Choose an Account</label>
    <select id="banks" name="banks">
        <option> value='{{ x }}'>{{x}}</option>
        {% endfor %}
    </select>
    <div class="form-group">
        <label for="Accounts">Account Number:</label>
    </div>
</form></div>{% endblock %}

However I keep encountering the error "jinja2.exceptions.UndefinedError: 'account' is undefined"

Here is my models.py for my bank table and account tables

class Bank(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.column(db.String(150))
    number = db.Column(db.Integer, unique=True)
    ownership = db.Column(db.String(150))
    type = db.Column(db.String(150))
    amount = db.Column(db.Integer)
    account_id = db.Column(db.Integer, db.ForeignKey('account.id'))

class Account(db.Model, UserMixin): #User object, inherit db from models.py, creating a schema
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(150), unique=True)
    password = db.Column(db.String(150))
    username = db.Column(db.String(150), unique=True)
    bank_accounts = db.relationship('Bank')

I have no idea where to start frankly, I am pretty new to making web apps and never used flask before this project. To me it seems that account is defined in the same manner as is set up in the video.

Any help with solving this issue would be greatly appreciated!

Cheers!

Edit: Here is my views.py which contains the route.

@views.route('/accounts', methods=['GET', 'POST'])
@login_required
def accounts():
    return render_template("accounts.html", user=current_user)

Does account need to be defined in the route? If so I have tried that but I wasn't able to use the select() method. It gave me this error: AttributeError: type object 'Bank' has no attribute 'select'

account = Bank.select(account_id)


Read more here: https://stackoverflow.com/questions/67389203/using-jinja-to-loop-through-a-db-table-getting-error-db-name-is-undefined

Content Attribution

This content was originally published by Nicholas at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: