zoukankan      html  css  js  c++  java
  • Django学习小记4-学员数据库管理②

    上一篇 Django学习小记3-学员管理数据库① 我们提到的是最基本的单表操作。实际还有一对多、多对多的案例。

    多表涉及到连表操作。

    理解:Form表单提交,页面会刷新。

    1. 查看

    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)

    所以在数据库语句就可以替换成:

    #展示学生
    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.添加

    在页面让用户添加所属班级时,可以设置一个下拉框,用<select><option>标签实现: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>

    注意:在select标签加入了class_id属性,这样提交的学生name作为key,class_id作为value来提交。

    views.py里添加add_student函数:

    ...
    #添加学生,下拉框
    def add_student(request):
        if request.method == "GET":
         #由数据库提取班级名称填充value到add_student.html的下拉框
    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 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/')

    其实这样每次都去操作数据库太繁琐了,可以把他们封装在utils目录下起名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)

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

    为美好的生活奋斗!
  • 相关阅读:
    对象数组、集合、链表(java基础知识十五)
    正则表达式、Calendar类、SimpleDateFormat类、Date类、BigDecimal类、BigInteger类、System类、Random类、Math类(Java基础知识十四)
    stringBuffer、StringBuilder、排序、Arrays、Jdk1.5新特性(java基础知识十三)
    Scanner、String(java基础知识十二)
    开发工具、Object类(java基础知识十一)
    包、修饰符、内部类、匿名内部类(java基础知识十)
    获取cookies的简单代码(总结待续)
    多态、抽象类、接口、区别(java基础知识九)
    代码块、继承、this、super、final(java基础知识八)
    构造方法,重载,static,math类(java基础知识七)
  • 原文地址:https://www.cnblogs.com/ethtool/p/12132472.html
Copyright © 2011-2022 走看看