首页 > 解决方案 > Jinja2/Flask,我无法从复选框中获取动态值

问题描述

所以我有这个html

<label class="continer">Empresa Confidencial
     {% if proyecto.10 == 1 %}
     <input type="checkbox" id="confidencial" name="confidencial" value="1" checked>
     {% else  %}
     <input type="checkbox" id="confidencial" name="confidencial" value="0">
     {% endif %}
     <span class="checkmark"></span>
</label>

项目.py

def editar_(confidencial,estado,id):
    cur = mysql.connection.cursor()
    cur.execute('UPDATE proyecto set EMPRESA_CONFIDENCIAL=%s,estado=%s where PK_PROYECTO=%s', (confidencial,estado, id,))
    mysql.connection.commit()

管理视图.py

@app.route("/editar", methods=["POST"])
def editar():
    print('.................................................................................')
    id = request.form['id']
    estado = request.form.get('estado') 
    confidencial = request.form.get('confidencial')
    print(id,str(estado), str(confidencial))
    pr.editar_(confidencial,estado,id)
    return redirect(url_for('empresasT'))

我需要获取值"1""0"将其发送到数据库,问题是当我运行代码时,没有"confidencial"inproyect.pyadminvie.py,它运行并在数据库中进行更改,但是当我再次尝试使用"confidencial", 并进行更改checkbox,如果复选框被选中并且我未选中它,则值为无,但如果我将其选中,则默认值为一个,控制台说该值为空,,MySQLdb._exceptions.OperationalError: (1048, "Column 'EMPRESA_CONFIDENCIAL' cannot be null")该过程没有得到值"1""0"来自checkbox修改后的值,但我得到的其他值如 id 和 estado 确实,我认为是if语句,我如何使用语句从checkboxhtml 中获取值if

标签: pythonwebflaskjinja2

解决方案


我认为您应该将选中的复选框添加到两个复选框中。


推荐阅读