Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何根据Flask中的复选框值更新SQLite表?_Python_Sqlite_Flask_Flask Sqlalchemy_Flask Wtforms - Fatal编程技术网

Python 如何根据Flask中的复选框值更新SQLite表?

Python 如何根据Flask中的复选框值更新SQLite表?,python,sqlite,flask,flask-sqlalchemy,flask-wtforms,Python,Sqlite,Flask,Flask Sqlalchemy,Flask Wtforms,我有一个SQLite数据库,其中包含有关产品的数据,如产品名称、说明和类似,显示用户是否喜欢该产品 用户搜索使用名称、说明和复选框填充表格的不同产品,如果数据库中的Like的值为1,则选中该复选框,否则不选中该复选框。 我已经通过实现了按钮(如index.html中所示) 我现在希望用户能够选中和取消选中表中的框,然后按下按钮更新数据库中的Like列,我该怎么做 (我甚至无法访问app.py中按钮的值。当尝试print(requests.form['like'])在name=requests.

我有一个SQLite数据库,其中包含有关产品的数据,如
产品名称
说明
类似
,显示用户是否喜欢该产品

用户搜索使用名称、说明和复选框填充表格的不同产品,如果数据库中的
Like
的值为1,则选中该复选框,否则不选中该复选框。 我已经通过实现了按钮(如
index.html
中所示)

我现在希望用户能够选中和取消选中表中的框,然后按下按钮更新数据库中的
Like
列,我该怎么做

(我甚至无法访问
app.py
中按钮的值。当尝试
print(requests.form['like'])
name=requests.form['search']
之后的行上打印(requests.form['like'])时,返回
BadRequestKeyError:浏览器发送了一个此服务器无法理解的请求。KeyError:'like'

app.py

... 
product = []
@app.route('/', methods = ['POST'])
def index():
   name = None
   if request.method == 'POST':
     name = request.form['search']
     if name is not None or name != "":
        query_product = Products.query.filter(Products.productname==name).first()
        if (query_product is not None) and (query_product not in product):
            company.append(query_company)
   print(company, file = sys.stderr)
   return render_template('index.html', companies = company)
产品

class Products(db.Model):
   index = db.Column(db.Integer(), primary_key = True)
   productname = db.Column(db.String(), primary_key = False)
   description = db.Column(db.String(), primary_key = False)
   like = db.Column(db.Integer(), primary_key = False)
   ...more columns
index.html

 <!-- /templates/index.html  -->

{% extends 'base.html' %}
{% block content %}
    <form method="POST">
        <input type="text" placeholder="Search for Product.." name="search">
        <button type="submit" name="submit">Search</button> <button type="submit" name="update" value = company.index style = margin-left:45%>Update</button>
    <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 {{company.index}}>
                        <th scope="row">{{ product.productname}}</th>
                        <td> {{ product.description }} </td>
                        <td><form method = "POST"> <input type="checkbox" name="like" {% if product.like == 1 %} checked {% else %} {% endif %}></form></td>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
{% endblock %}

{%extends'base.html%}
{%block content%}
搜索更新
品名
描述
喜欢
{products%中产品的%s}
{{product.productname}
{{product.description}}
{%endfor%}
{%endblock%}

我想您需要使用javascript完成这项工作,并向后端添加另一个路由,然后更新数据库

可能是这样的,如果它自动发生:

<input type="checkbox" onchange="updateLike('productId', this.checked)">
<script>
async function updateLike(productId, doesLike) {
    let response = await fetch("http://localhost/products/productId/like", {
        method:"POST",
        headers: {"Content-Type":"application/json"},
        body: JSON.stringify({
            productId: productId,
            like: doesLike
        })
    });
}
</script>

异步函数updateLike(productId,doesLike){
let response=等待获取(“http://localhost/products/productId/like", {
方法:“张贴”,
标题:{“内容类型”:“应用程序/json”},
正文:JSON.stringify({
productId:productId,
比如:杜斯利克
})
});
}
或者您可以添加一个按钮,将请求发送到服务器

<input type="checkbox" name="like"/>
<button onclick="updateLike('productId', document.querySelector('input[name=like]').checked)">confirm</button>

确认

你好,谢谢。我试试这个。如何指定应该更新的是我的SQLite数据库?哪个是SQLAlchemy()db对象?对不起,我从来没有听说过SQLAlchemy。但也许这有助于: