繁体   English   中英

如何通过基于使用 Flask 的搜索查询现有 SQLite 表来填充 HTML 表?

[英]How to populate a HTML table by querying existing SQLite table based on search using Flask?

我有一个 SQLite 表,其中存储了多个列,其中 3 个是Product NameDescriptionLike 我希望用户能够搜索产品名称,将产品数据添加为 HTML 表中的行,然后按用户是否喜欢该产品。 这应该会更新所选产品的 SQLite Like列。

正如您在下面的代码中看到的,我正在尝试获取用户输入的“名称”,并过滤 SQLite 表中的数据并将其打印在表中。 但我不知道如何编写过滤 SQLite 表的行,如下所示。

app.py

from flask import Flask, request, jsonify, render_template, redirect
import os
from models import db, Products

app = Flask(__name__)

# Create dummy secrey key so we can use sessions
app.config['SECRET_KEY'] = '123456790'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
# Create in-memory database
app.config['DATABASE_FILE'] = 'mna.db'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + app.config['DATABASE_FILE']
db.init_app(app)

@app.route('/', methods = ['GET'])
def index():
   products = []
   name = request.args.get('search')
   products.append(Products.productname == name)  ##<-- I don't know how to write this line correctly
   return render_template('index.html', products=products)

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

models.py

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class Products(db.Model):

   productid= db.Column(db.String(), primary_key=True)
   productname= db.Column(db.String(), primary_key=False)
   description = db.Column(db.String(), nullable=False)
   like = db.Column(db.Boolean(), nullable=False)

def __str__(self):
    return unicode(self).encode('utf-8')

def __unicode__(self):
    return "Name: {productname}; Description : {description}".format(productname=self.productname, description=self.description)

还有我的index.html

{% extends 'base.html' %}

{% block content %}
    <form method="GET">
        <input type="text" placeholder="Search for Product.." name="search">
        <button type="submit"><i class="search-container"></i></button>
    </form>
    <div class="product-container" style="overflow: auto; max-height: 80vh">
        <div class="table-responsive">
            <table class="table" id="products">
                <thead>
                    <tr>
                        <th scope="col">Product Name</th>
                        <th scope="col">Description</th>
                        <th scope="col">Like</th>
                    </tr>
                </thead>
                <tbody>
                {% for product in products %}
                    <tr ={{ product .productid}}>
                        <th scope="row">{{ product .productname}}</th>
                        <td> {{ product.description }} </td>
                        <td> {{ product.like }} </td>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
{% endblock %}

您需要使用来自SQLAlchemy的查询接口,

使用任何一个,

  1. 过滤
  2. 过滤

尝试,

db.session.query(Product).filter(Product.productname == name)
or
db.session.query(Product).filter_by(productname = name)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM