Kirishni HTML formatidan olish va uni MySQL-da Flask orqali saqlash

Pythonda birinchi qadamlarni olaman. Flask ramkasidan foydalanaman va HTML shaklidan kirishni va MySQL ma'lumotlar bazasida saqlashga imkon beradigan asosiy dasturni yozyapman. Shunday qilib, men shuni qilmoqdaman

HTML form sample.html:

 <!DOCTYPE HTML>
  <html>
  <body>

  <form method="POST">
  First name: <input type="text" name="fname">
Last name: <input type="text" name="lname">
Email Id : <input type="text" name="emailid">
<input type="submit" value="Submit"> </form> </body> </html>

Python skriptini ishlataman:

from flask import Flask,request,render_template
from flaskext.mysql import MySQL
mysql=MySQL()

app=Flask(__name__)
app.config['MYSQL_DATABASE_USER']='root'
app.config['MYSQL_DATABASE_PASSWORD']='root'
app.config['MYSQL_DATABASE_DB']='names'
app.config['MYSQL_DATABASE_host']='127.0.0.1:3306'
mysql.init_app(app)

@app.route('/',methods=['GET','POST'])
def get_data():
 return render_template("sample.html")
  if request.method=='POST':
    first_name=request.form['fname']
    last_name=request.form['lname']
    emailid=request.form['emailid']
    connection = mysql.get_db()
    cursor = connection.cursor()
 query="INSERT INTO names_tbl(f_name,l_name,e_id) VALUES(%s,%s,%s)"
 cursor.execute(query,(first_name,last_name,email_id))
    connection.commit()
    return "nothing fucked"
else:
    return("something fucked up")

if __name__=='__main__':
app.run(debug=True)
2
Sizning qilishingiz kerak bo'lgan birinchi narsa kodingiz indent
qo'shib qo'ydi muallif e4c5, manba
Sizning qilishingiz kerak bo'lgan birinchi narsa kodingiz indent
qo'shib qo'ydi muallif e4c5, manba
Shubhasiz, bu ishlamaydi. Men bilmoqchi bo'lgan narsa 1 edi. O'zgaruvchan HTML-ni to'g'ri qabul qilishim kerakmi? 2) muvaffaqiyatli kiritilgan ekranda "muvaffaqiyatli saqlangan dbda saqlangan" ni chop qilmoqchi bo'lsangiz, nima qilishim kerak?
qo'shib qo'ydi muallif Anoop Vijayan, manba

6 javoblar

import MySQLdb

db = MySQLdb.connect(
    host = 'localhost',
    user = 'root',
    passwd = 'pw',
    db = 'mydB',
    charset='utf8'
    )

@app.route('/',methods=['GET','POST'])
def get_data():
 return render_template("sample.html")
  if request.method=='POST':
    first_name=request.form['fname']
    last_name=request.form['lname']
    emailid=request.form['emailid']
    cursor = db.cursor()
    cursor.execute("""
    INSERT INTO names_tbl(f_name,l_name,e_id) \
    VALUES (%s,%s,%s) """, (first_name,last_name,email_id))
    cursor.close()
    return "nothing fucked"

if __name__=='__main__':
app.run(debug=True)
0
qo'shib qo'ydi
Ma'lumotlar bazasida saqlanmasligi yoki yo'qligini ko'rish uchun nima o'rnatishim kerak. Men shishadagi yangi odamman, menga kimdir yordam berishi mumkin.
qo'shib qo'ydi muallif user_123, manba
import MySQLdb

db = MySQLdb.connect(
    host = 'localhost',
    user = 'root',
    passwd = 'pw',
    db = 'mydB',
    charset='utf8'
    )

@app.route('/',methods=['GET','POST'])
def get_data():
 return render_template("sample.html")
  if request.method=='POST':
    first_name=request.form['fname']
    last_name=request.form['lname']
    emailid=request.form['emailid']
    cursor = db.cursor()
    cursor.execute("""
    INSERT INTO names_tbl(f_name,l_name,e_id) \
    VALUES (%s,%s,%s) """, (first_name,last_name,email_id))
    cursor.close()
    return "nothing fucked"

if __name__=='__main__':
app.run(debug=True)
0
qo'shib qo'ydi
Ma'lumotlar bazasida saqlanmasligi yoki yo'qligini ko'rish uchun nima o'rnatishim kerak. Men shishadagi yangi odamman, menga kimdir yordam berishi mumkin.
qo'shib qo'ydi muallif user_123, manba
import MySQLdb

db = MySQLdb.connect(
    host = 'localhost',
    user = 'root',
    passwd = 'pw',
    db = 'mydB',
    charset='utf8'
    )

@app.route('/',methods=['GET','POST'])
def get_data():
 return render_template("sample.html")
  if request.method=='POST':
    first_name=request.form['fname']
    last_name=request.form['lname']
    emailid=request.form['emailid']
    cursor = db.cursor()
    cursor.execute("""
    INSERT INTO names_tbl(f_name,l_name,e_id) \
    VALUES (%s,%s,%s) """, (first_name,last_name,email_id))
    cursor.close()
    return "nothing fucked"

if __name__=='__main__':
app.run(debug=True)
0
qo'shib qo'ydi
Ma'lumotlar bazasida saqlanmasligi yoki yo'qligini ko'rish uchun nima o'rnatishim kerak. Men shishadagi yangi odamman, menga kimdir yordam berishi mumkin.
qo'shib qo'ydi muallif user_123, manba
@app.route('/',methods=['GET','POST'])
def get_data():
  if request.method=='POST':
    first_name=request.form['fname']
    last_name=request.form['lname']
    emailid=request.form['emailid']
    connection = mysql.get_db()
    cursor = connection.cursor()
    query="INSERT INTO names_tbl(f_name,l_name,e_id) VALUES(%s,%s,%s)"
    cursor.execute(query,(first_name,last_name,email_id))
    connection.commit()

 return render_template("sample.html")

if __name__=='__main__':
app.run(debug=True)
0
qo'shib qo'ydi
@app.route('/',methods=['GET','POST'])
def get_data():
  if request.method=='POST':
    first_name=request.form['fname']
    last_name=request.form['lname']
    emailid=request.form['emailid']
    connection = mysql.get_db()
    cursor = connection.cursor()
    query="INSERT INTO names_tbl(f_name,l_name,e_id) VALUES(%s,%s,%s)"
    cursor.execute(query,(first_name,last_name,email_id))
    connection.commit()

 return render_template("sample.html")

if __name__=='__main__':
app.run(debug=True)
0
qo'shib qo'ydi
@app.route('/',methods=['GET','POST'])
def get_data():
  if request.method=='POST':
    first_name=request.form['fname']
    last_name=request.form['lname']
    emailid=request.form['emailid']
    connection = mysql.get_db()
    cursor = connection.cursor()
    query="INSERT INTO names_tbl(f_name,l_name,e_id) VALUES(%s,%s,%s)"
    cursor.execute(query,(first_name,last_name,email_id))
    connection.commit()

 return render_template("sample.html")

if __name__=='__main__':
app.run(debug=True)
0
qo'shib qo'ydi
Python
Python
372 ishtirokchilar

Bu guruh python dasturlash tilini muhokama qilish uchun. Iltimos, o'zingizni hurmat qiling va faqat dasturlash bo'yicha yozing. Botlar mavzusini @botlarhaqida guruhida muhokama qling! FAQ: @PyFAQ Offtopic: @python_uz_offtopic

Python offtopic group !
Python offtopic group !
150 ishtirokchilar

@python_uz gruppasining offtop gruppasi. offtop bo'lsa ham reklama mumkin emas ) Boshqa dasturlash tiliga oid gruppalar @languages_programming