Python Forum
Read Save RadioButtons from Database in Python Flask Webpage
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read Save RadioButtons from Database in Python Flask Webpage
#1
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask read real time print from subprocess without deadlock vofka32 0 961 Jun-02-2021, 09:36 AM
Last Post: vofka32
  Help using Flask for a countdown on webpage ZenBuddhism 1 838 Feb-05-2021, 03:35 PM
Last Post: snippsat
  Using range slider in flask webpage to use in python KimPet 2 2,431 Jan-23-2021, 11:58 PM
Last Post: snippsat
  Flask app cannot read js and css files. Aggam 0 637 Nov-04-2020, 12:33 PM
Last Post: Aggam
  Flask Can't Save Screenshot to Postgres Db firebird 3 915 Sep-21-2020, 09:22 PM
Last Post: firebird
Question wkhtmltoimage generated jpeg save to Database using mongoengine Madoo 2 1,098 Aug-18-2020, 03:42 PM
Last Post: Madoo
  python 3.7 on windows using flask and flask-sqlalchemy. Alpy 2 2,387 Aug-12-2020, 07:24 PM
Last Post: Alpy
  Flask export/upload database table in cvs/xlsx format steve87bg 4 2,762 Jun-19-2020, 01:46 PM
Last Post: steve87bg
  Python values on a WebPage Petrouil 1 922 Apr-01-2020, 05:08 PM
Last Post: ndc85430
  how to save the data from MySQL to CSV in Flask farah97 4 1,442 Jan-03-2020, 03:02 AM
Last Post: farah97

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020