4- Students learn Django small note Database Management ②

Previous  Django learning little note 3- student management database ①  we mentioned the most basic single-table operation. There are actually many, many to many cases.

It relates to multi-table connected to the operating table.

Understand: Form the form is submitted, the page refreshes.

1. View

mysql> SELECT * FROM student LEFT JOIN class ON student.`class_ID`=class.`id`;
+----+-----------+----------+------+-----------------+
| id | name      | class_ID | id   | title           |
+----+-----------+----------+------+-----------------+
|  1 | 张英杰    |        9 |    9 | 全栈12期1222    |
+----+-----------+----------+------+-----------------+
1 row in set (0.00 sec)

mysql> SELECT student.id,student.name,class.title FROM student LEFT JOIN class ON student.`class_ID`=cllass.`id`;
+----+-----------+-----------------+
| id | name | title |
+----+-----------+-----------------+
| 1 | 张英杰 | 全栈12期1222 |
+----+-----------+-----------------+
1 row in set (0.00 sec)

So in the database can be replaced with the statement:

#展示学生
def students(request):
    import pymysql
    conn = pymysql.connect(host='10.0.4.x', port=3306, user='root', passwd='123@#123', db='oldboys',charset='utf8')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cursor.execute("SELECT student.id,student.name,class.title FROM student LEFT JOIN class ON student.`class_ID`=class.`id`")
    student_list = cursor.fetchall()
    cursor.close()
    conn.close()

    return render(request,'students.html',{'student_list':student_list})

 

2. Add

When the page allows users to add your class, you can set up a drop-down box, <option> tag implemented <select>: add_student.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加学生</title>
</head>
<body>
    <h1>增加学生</h1>
    <form method="POST" action="/add_student/">
        <p>学生姓名<input type="text" name="name"/></p>
        <p>所属班级:
            <select name="class_id">
                {% for row in class_list %}
                    <option value="{{ row.id }}">{{ row.title }}</option>>
                {% endfor %}
            </select>
        </p>
        <input type="submit" value="提交">
    </form>
</body>
</html>

Note: select the label joined class_id property, so students submitted name as key, class_id to submit as value.

views.py was added add_student function:

...
 # add students, drop-down box 
DEF add_student (Request):
     IF request.method == " GET " : 
     # filled by the database to extract the class name drop-down box value to add_student.html
Import pymysql conn = Host pymysql.connect (= ' 10.0.4.x ' , Port = 3306, = User ' the root ' , the passwd = ' 123! @ # 123 ' , DB = ' oldboys ' , charset = ' UTF8 ' ) Cursor = conn.cursor (= Cursor pymysql. cursors.DictCursor) Cursor. the Execute ( "select id,title from class") class_list = cursor.fetchall() cursor.close() conn.close() return render(request,'add_student.html',{'class_list':class_list}) else: name = request.POST.get('name') class_id = request.POST.get('class_id')      #提交至数据库 import pymysql conn = pymysql.connect(host='10.0.4.x', port=3306, user='root', passwd='123!@#123', db='oldboys',charset='utf8') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) cursor.execute("insert into student(name,class_id) values(%s,%s)",[name,class_id]) conn.commit() cursor.close() conn.close() return redirect('/students/')

In fact, every time too cumbersome to operate the database, you can put them in a package named utils directory sqlheper.py

 sqlheper.py

import pymysql

#fetchall
def get_list(sql,args):
    conn = pymysql.connect(host='10.0.4.x', port=3306, user='root', passwd='123!@#123', db='oldboys',charset='utf8')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cursor.execute(sql,args)
    result = cursor.fetchall()
    cursor.close()
    conn.close()
    return result

#fetchone
def get_one(sql,args):
    conn = pymysql.connect(host='10.0.4.x', port=3306, user='root', passwd='123!@#123', db='oldboys',charset='utf8')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cursor.execute(sql,args)
    result = cursor.fetchone()
    cursor.close()
    conn.close()
    return result

#commit
def modify(sql,args):
    conn = pymysql.connect(host='10.0.4.x', port=3306, user='root', passwd='123!@#123', db='oldboys',charset='utf8')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cursor.execute(sql,args)
    conn.commit()
    cursor.close()
    conn.close()

 

 

 

Ajax (jQuery)

“页面不刷新的情况下偷偷提交数据”

Guess you like

Origin www.cnblogs.com/ethtool/p/12132472.html