django -----原生SQL语句查询与前端数据传递?

view.py中

import MySQL

def request_data(request):
    if request.method == "GET":
        conn = MySQLdb.Connect(
                host ='my_ip',
                port = 3306,
                user = 'my_user',
                passwd = 'my_passwd',
                db = 'my_db',
                charset = 'utf8'
        )
        cursor = conn.cursor()
        cursor.execute("select * from my_table limit 1")
        values=cursor.fetchall()

        print(values)

        cursor.close()
        conn.close()
        return render(request,"data.html",{'results':values})
def GetList(sql):
    db = MySQLdb.connect(user='root', db='wupeiqidb', passwd='1234', host='localhost')
    cursor = db.cursor()
    cursor.execute(sql)
    data = cursor.fetchall()
    db.close()
    return data
原文地址:https://www.cnblogs.com/lmh001/p/9794955.html