Python Forum

Full Version: Read Save RadioButtons from Database in Python Flask Webpage
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am new to Python, Flask & HTML. I have a SQL Server table Products that contains DeptManager, Product & SellStatus (which could be yes, no or not sure). Table Structure:

[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]


I need to create a web page for each department manager, where they see a list of their own products and they can choose if they still sell the product or not and change the status if needed.

So basically I need to generate a radio button from values in the database table column - a radio button group per row with values yes, no and not sure. It should read the current product and populate current sellstatus value in the radio button group for each product from the database table and then when the department manager chooses different values and clicks Submit, it should insert new values in the same table.

I was able to create a webpage that can display product names and radio buttons. But I couldn't read the radio button values from the database table.

Python Code:
-----------------
        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):
            CaStatus = SelectField('', 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 Code:
-------------

<!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="ca_{{loop.index}}" value="Yes" checked> Yes &nbsp;&nbsp;&nbsp;
<input type="radio" name="ca_{{loop.index}}" value="No"> No &nbsp;&nbsp;&nbsp;
<input type="radio" name="ca_{{loop.index}}" value="Not Sure"> Not Sure &nbsp;&nbsp;&nbsp;
</td>
<br><br>
</tr>
{% endfor %}
{{form.Submit()}}
</form>
</body>
</html>



I am currently getting a list of products for a particular manager with radio buttons, all defaulted to Yes. E.g.


Do you still sell these products?


Sony 55' TV (x)Yes ()No ()Not Sure

Panasonic 55' TV (x)Yes ()No ()Not Sure

LG DVD Player (x)Yes ()No ()Not Sure

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

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


But I need to read these from the table and I so far couldn't figure out how to link the values with the table. And when the user clicks Submit, update the new values in the table. Please help. Thanks in advance!