[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Python Information Form

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



<form method="POST" action="{{ url_for('index') }}">

Name <input type="text" name="name" />

Email <input type="email" name="email" />
CRType <select name = "CRType">
        <option value = "New">New</option>
         <option value = "Old">Old</option>
<input type="submit" value="Submit">

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))
        return redirect ('/results')
      #  return redirect('/results')
    return render_template('index.html')

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>

<form method="POST", action="{{ url_for('Edit') }}">

CR_ID <input type="text" name="CR_ID" value = "{{user[0]}}"/>
Name <input type="text" name="name" value = "{{user[1]}}"/>

Email <input type="email" name="email" value = "{{user[2]}}"/>
      <input type="submit" value="Submit">

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()
        user = RV[0]
        return render_template('Edit.html',user=user)

Thanks in Advance.