How to implement filters in Flask

Viewed 30

Can you help me find informations about implementing filters in my online shop project? My shop current endpoint looks like "http://127.0.0.1:5000/shop" and I would like to end up with something like "http://127.0.0.1:5000/shop?gender="female"&color="black"&color="white"&sort_by="price". Filters would be optionally choosen with buttons and checkboxes, and item list would be filtered with this criterias. My currenty main.py:

from flask import Flask, render_template
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

# CONNECT WITH DATABASE
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///online_shop.db"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)


# DATABASE STRUCTURE
class Item(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    brand = db.Column(db.String(32), nullable=False)
    model = db.Column(db.String(32), nullable=False)
    gender = db.Column(db.String(8), nullable=False)
    color = db.Column(db.String(16), nullable=False)
    stock_39 = db.Column(db.Integer, nullable=True)
    stock_40 = db.Column(db.Integer, nullable=True)
    stock_41 = db.Column(db.Integer, nullable=True)
    price = db.Column(db.Integer, nullable=False)
    desc = db.Column(db.String(512), nullable=True)
    params = db.Column(db.String(512), nullable=True)
    img1 = db.Column(db.String(128), nullable=True)
    img2 = db.Column(db.String(128), nullable=True)


all_items = db.session.query(Item).all()


@app.route('/')
def homepage():
    return render_template("index.html", current_year=datetime.now().year)


@app.route('/shop')
def shop():
    return render_template("shop.html", current_year=datetime.now().year, all_items=all_items)


@app.route('/shop/<int:item_id>')
def item(item_id):
    item = Item.query.filter_by(id=item_id).first()
    return render_template("item.html", current_year=datetime.now().year, item=item)


@app.route('/about')
def about():
    return render_template("about.html", current_year=datetime.now().year)


@app.route('/contact')
def contact():
    return render_template("contact.html", current_year=datetime.now().year)


if __name__ == "__main__":
    app.run(debug=True)

# COMMIT CHANGES TO DATABASE
db.session.commit()

And my shop page for better understanding: I don't really know how to name my problem to google it, I am sure it's a common question. You can drop me some links or tips what should I google or even your idea of how to do it. I am beginer at Flask.

0 Answers
Related