2

I have a form in my flask application that collects emails, usernames, and phone numbers. I have stored them all in the SQLite database using flask sqlalchemy. Let's say my database looks like the following:

Email                    Username                     Phone Number
[email protected]           James                          123456789
 [email protected]            Jane                          987654321

I have an HTML file and I would like to display data there. What I have is :

> @app.route("/Display") 
  def Display():
     # Here I want to get all data from the database
     # like this data=get from database 
     return render_template('Display.html', data=data)

I would like to know can anyone tell me how to retrieve data or get the data from database using flask sqlalchemy ORM. I know query.filter_by can be used to filter according to a username or email or phone number. However, how can I get the whole table (like in a form to pass to render_template? like in HTML page I want to loop through data and use data.username or data.email)

2 Answers 2

2

You can use this code to retrieve everything

data = Model.query.all()

Where model is the name of your model. This returns a list of the objects.

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

Comments

1

If you want to filter the data by a specific value:

data = Model.query.filter_by(attribute=value)

You can add .all() if you want to get all instances of your Model matching the value.

If you want to filter by the ID of the data:

data = Model.get_or_404(dataID)

Comments

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.