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: sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) database is locked pythonpaul32 1 2,027 Apr-04-2023, 07:44 AM
Last Post: Larz60+
  Flask and SQLAlchemy question: Database is being created but tables aren't adding pythonpaul32 3 4,410 Feb-07-2023, 10:48 AM
Last Post: pythonpaul32
  Flask/non-flask database sharing MorganSamage 2 1,133 Feb-03-2023, 12:05 PM
Last Post: MorganSamage
  Flask, read H5 text value tantony 4 1,391 Oct-31-2022, 05:29 PM
Last Post: tantony
  Save JSON data to sqlite database on Django Quin 0 2,804 Mar-26-2022, 06:22 PM
Last Post: Quin
  Flask read real time print from subprocess without deadlock vofka32 0 4,016 Jun-02-2021, 09:36 AM
Last Post: vofka32
  Help using Flask for a countdown on webpage ZenBuddhism 1 2,187 Feb-05-2021, 03:35 PM
Last Post: snippsat
  Using range slider in flask webpage to use in python KimPet 2 7,525 Jan-23-2021, 11:58 PM
Last Post: snippsat
  Flask app cannot read js and css files. Aggam 0 1,600 Nov-04-2020, 12:33 PM
Last Post: Aggam
  Flask Can't Save Screenshot to Postgres Db firebird 3 2,341 Sep-21-2020, 09:22 PM
Last Post: firebird

Forum Jump:

User Panel Messages

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