在Python Flask网页上阅读从数据库保存单选按钮

时间:2019-04-10 22:07:04

标签: python database flask radio-button pymssql

我是Python,Flask和HTML的新手。我有一个包含DeptManager,Product和SellStatus的SQL Server表Products(可能是,否或不确定)。表格结构:

[ManagerGUID], [ManagerName], [ProductName], [SellStatus]
------------------------------------------------------------
[wqew-2324-4543], [Sandy],[Sony 55 inch TV], [Not Sure]

[wqea-1324-4533], [Sandy],[Sony 65 inch TV], [Yes]

[wqev-2354-4523], [Sandy],[LG 55 inch TV], [No]

[wqsd-2124-4573], [Andy],[LG Stove], [Not Sure]

[wafw-2374-4548], [Andy],[Kitchen Aid Fridge], [Yes]

我需要为每个部门经理创建一个网页,他们在其中看到自己的产品列表,他们可以选择是否仍然销售产品,并根据需要更改状态。

因此,基本上,我需要根据数据库表列中的值生成一个单选按钮-每行的单选按钮组的值是,否和不确定。它应该读取当前产品,并从数据库表中为每个产品填充单选按钮组中的当前sellstatus值,然后,部门经理选择不同的值并单击Submit(提交)时,应在同一表中插入新值。

我能够创建一个可以显示产品名称和单选按钮的网页。但是我无法从数据库表中读取单选按钮值。

Python代码:

    from flask import Flask, render_template, session, redirect, url_for, request

    import pymssql

from flask_wtf import FlaskForm

    from wtforms import Form, validators, RadioField, SelectField, SubmitField

    from wtforms.validators import data_required

    app = Flask(__name__)

    app.config['SECRET_KEY'] = 'mykey'

    class InfoForm(FlaskForm):
        SellStatus = RadioField('', choices=[('Yes', 'Yes'), ('No', 'No'), ('Not Sure', 'Not Sure')])
        Submit = SubmitField('Submit')

    @app.route('/<BGUID>', methods=['POST', 'GET'])

    def index(BGUID):
       #option = request.form['options']
       conn = pymssql.connect(server='MySQLServer', user='MyUsr', password='MyPaswd', database='MySQLDB')

       cursor = conn.cursor()

       cursor.execute('SELECT [ManagerName], [ProductName], [SellStatus] FROM [dbo].[ProductStatus] WHERE b.[ManagerGuid] = %s', BGUID)

       data = cursor.fetchall()

       conn.close()

       form = InfoForm()

       if request.method =='GET':
        return render_template('basic.html', data = data, form = form)

    if __name__ == '__main__':

        app.run(debug=True)

HTML代码:

<!DOCTYPE html>
<html lang="en" dir = "ltr">
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <h2>Do you still sell these products?</h2>
        <form method = "post">
        {{form.hidden_tag()}}
        {% for item in data %}
        <tr>
         <td>{{item[1]}}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
         <td>
            <input type="radio" name="ss_{{loop.index}}" value="Yes" checked> Yes &nbsp;&nbsp;&nbsp;
            <input type="radio" name="ss_{{loop.index}}" value="No"> No &nbsp;&nbsp;&nbsp;
            <input type="radio" name="ss_{{loop.index}}" value="Not Sure"> Not Sure &nbsp;&nbsp;&nbsp;
        </td>
        <br><br>
    </tr>
    {% endfor %}
    {{form.Submit()}}
    </form>
    </body>
</html>

我目前正在通过单选按钮获取特定经理的产品列表,所有按钮均默认为“是”。例如

您仍然出售这些产品吗?

Sony 55'TV(x)是()否()不确定

Panasonic 55'电视(x)是()否()不确定

LG DVD Player(x)是()否()不确定

.............

.............

但是我需要从表中读取这些信息,到目前为止,我仍然无法弄清楚如何将值与表链接。当用户单击“提交”时,更新表中的新值。请帮忙。预先感谢!

0 个答案:

没有答案