Among these tools, SQLAlchemy stands out as a powerful and elegant solution that simplifies database interactions for Python developers. Python, one of the most popular and versatile programming languages, has a rich ecosystem of tools and libraries to interact with databases efficiently and effectively. See ya.Databases are the lifeblood of modern applications, powering everything from simple blogs to complex e-commerce platforms. Next time, I would like to design and style our product list so I work a lot with Jinja2 template tags, Bootstrap 4 snippets and CSS style. At least I filled my database with dummy data and displayed it on my front end. Quickly add template tags into template file landingLayout.html. return render_template('landingpage/landingLayout.html', title='Home', user=user, products=products) Let’s pass query result to our render template function. I am exhausted, but the final step should be easy and I will prove myself, that I can render query results on my webpage. The last step for today is to print query results in our template. Now my site works again and I even see query results printed into the console. To prove this theory, I edited the DB config relative path to look for the SQLite database file in the project folder instead. That is why the ORM query couldn’t find the Products table. Additionally, my DB config points to an empty SQLite database file in an app folder. And Flask created its own empty SQLite database file in an app folder. My hunch tells me, that Flask-Migrate creates an SQLite database file with all migration in the project folder. I had an SQLite database file both in the main project folder and the app folder. Long story short, after two hours of trials and errors I noticed a strange thing. I have no idea what does it means, but I decide to stick with Flask-SQLAlchemy official guide and change ORM query back to: () AttributeError: 'SQLAlchemy' object has no attribute ‘query' Try to change query syntax again without calling the session object. Yeah, my ORM query syntax might be wrong, or my Products table does not exist in the database. : (sqlite3.OperationalError) no such table: I am out of ideas and out of desperation, I try to revert back ORM query syntax. Try different paths for imports.Īfter all, my models.py resides inside the app folder. ModuleNotFoundError: No module named ‘models' The best way to find the root cause is to look into Gunicorn logs. Bad gateway means there is an issue somewhere between Nginx and Gunicorn. Maybe I forgot to import DB models into routes.py? My query uses models, right? from models import Productįinally another error. products = db.session.query(Product).all() Then change query syntax to use imported DB object. Let’s fix it and add import into routes.py. I added ORM query Product.all() and printed results into console for debugging purposes.īut when I try to refresh the browser, it throws an internal server error.Īccording to the logs, I forgot to import the SQLAlchemy object into Flask. Return render_template('landingpage/landingLayout.html', title='Home', user=user) With the help of this guide, I will try to add my first query to my index view. Now, after I filled my SQLite tables with some dummy data, I need to figure out, how to query them in Flask router views and finally display them in the template. I will do that manually with a Mac SQLite client DB Browser for Mac. Today, I would like to finally fill my SQLite database, that I configured last time, with some dummy data. Part 12: Building MVP with Flask Day 18-21 – Migration from SQLite to PostgreSQL.Part 11: Building MVP with Flask Day 10-17 – Adding Flask routes and views.Part 10: Building MVP with Flask Day 9 – Sorting with ORM queries.Part 9: Building MVP with Flask Day 8 – Serving static files. Part 8: Building MVP with Flask Day 7 – Left join with SQLAlchemy.Part 7: Building MVP with Flask Day 6 – Using Queryset to retrieve and render data.Part 6: Building MVP with Flask Day 5 – Setup SQLite database.Part 5: Building MVP with Flask Day 4 – Using children templates.Part 4: Building MVP with Flask Day 3 – Adding Bootstrap 4.Part 3: Building MVP with Flask Day 2 – Jinja2 template basics.Part 2: Building MVP with Flask Day 1 – Dev setup.Part 1: Building MVP with Flask Day 0 - Project goals.This post is part of the series Building an MVP with Flask.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |