On 2018-08-19 15:41, Suman Mupparapu wrote:
Hi Team,
I am newbie to Python and glad to be part of the team. Sorry for starting
with a help.
Working on a creating a small information form ..and encountering issues
when trying to edit the details using Python Flask and MYSQL DB.
Placed the code below for your reference. Please help to fix this issue.
Let me know if you need any other details from my end.
1) HTML Page for the Form
<h1>Welcome</h1>
<br>
<br>
<form method="POST" action="{{ url_for('index') }}">
Name <input type="text" name="name" />
<br>
Email <input type="email" name="email" />
<br>
CRType <select name = "CRType">
<option value = "New">New</option>
<option value = "Old">Old</option>
</select><br>
<input type="submit" value="Submit">
</form>
The Output of the page is as below
The Python Code to Save and Display results is as below
From flask import Flask, render_template, request, redirect
from flask_mysqldb import MySQL
# from flask_table import Table, Col, LinkCol
app = Flask(__name__)
# Configure db
#db = yaml.load(open('db.yaml'))
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
#app.config['MYSQL_PASSWORD'] = 'P@$$w0rd'
app.config['MYSQL_DB'] = 'flaskapp'
mysql = MySQL(app)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
# Fetch form data
userDetails = request.form
name = userDetails['name']
email = userDetails['email']
CRType = userDetails['CRType']
# CR_ID = userDetails['CR_ID']
cur = mysql.connection.cursor()
# cur.execute("""INSERT INTO users(name, email, CRType)
VALUES(%s, %s, % )""",(name, email, CRType)
cur.execute("""INSERT INTO users (name, email, CRType) VALUES
(%s, %s, %s)""", (name, email, CRType))
mysql.connection.commit()
cur.close()
return redirect ('/results')
# return redirect('/results')
return render_template('index.html')
@app.route('/results')
def results():
cur = mysql.connection.cursor()
resultValue = cur.execute("SELECT * from users")
if resultValue > 0:
userDetails = cur.fetchall()
# edit = LinkCol('Edit', 'edit', url_kwargs=dict(id='CR_ID'))
return render_template('results.html',userDetails=userDetails)
Results are displayed as below
If we click on the Edit for Example Row 1 .. the following data is displayed
When I click on Submit, the below error is displayed
Edit HTML Page and Python Code for Edit
h1>Welcome to Update Zone</h1>
<br>
<br>
<body>
<form method="POST", action="{{ url_for('Edit') }}">
CR_ID <input type="text" name="CR_ID" value = "{{user[0]}}"/>
<br>
Name <input type="text" name="name" value = "{{user[1]}}"/>
<br>
Email <input type="email" name="email" value = "{{user[2]}}"/>
<br>
<br>
<input type="submit" value="Submit">
</body>
</form>
app.route('/Edit', methods=['GET', 'POST'])
def Edit():
CR_ID = request.args.get('CR_ID')
cur = mysql.connection.cursor()
result = cur.execute("""SELECT CR_ID, name, email from users where
CR_ID = %s""",CR_ID)
RV = cur.fetchall()
cur.close()
user = RV[0]
return render_template('Edit.html',user=user)
Thanks in Advance.
I'm not seeing the results. Are you posting from Google Groups and
including images?
Google Groups is merely a mirror to the actual python-list group. Images
(and spam) are removed when they arrive at python-list, so only those
who are on Google Groups will see them.
--
https://mail.python.org/mailman/listinfo/python-list