运维开发网
广告位招商联系QQ:123077622
 
广告位招商联系QQ:123077622

day-58Django

运维开发网 https://www.qedev.com 2020-03-05 07:25 出处:网络 作者:运维开发网整理
项目 做一个小型的学员管理系统班级表(classes):id name 1 脱产8期 2 脱产9期学生表(students):id name class_id (外键关联班级ID)1    李晨       1 2    woge     1 3   刘伟     

项目

做一个小型的学员管理系统

班级表(classes):

id name

1 脱产8期

2 脱产9期

学生表(students):

id name class_id (外键关联班级ID)

1    李晨       1

2    woge     1

3   刘伟        2

老师表(teacher):

id   name

1    zekai

2    lxx

3    eagon

teacher2class(对应关系表):

id tid(外键teacher) cid (外键classes)

1      1     1

2      1      2

3      2      1

单表(班级和老师表):

- 查

- 增

- 删

- 改

一对多(学生表):

- 增

- 删

- 改

- 查

多对多(teacher2class):

- 增

- 删

- 改

- 查

服务端主逻辑

from django.conf.urls import url
from django.contrib import admin
from django.shortcuts import HttpResponse, render, redirect

==================新url方式,增删改查================
### 查看
def classes(request):
    import pymysql
    conn = pymysql.connect(host=‘127.0.0.1‘, user=‘root‘,  password=‘123‘, db=‘s8day58‘, charset=‘utf8‘)
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    sql = "select * from classes"
    cursor.execute(sql)
    classes = cursor.fetchall()

    return render(request, "classes.html", {"classes":classes})

### 添加  
def add_class(request):                                             #分流获取
    if request.method == ‘GET‘:
        return render(request, "add_class.html")
    else:
        classname = request.POST.get(‘classname‘)
        print(classname)
        import pymysql
        conn = pymysql.connect(host=‘127.0.0.1‘, user=‘root‘, password=‘123‘, db=‘s8day58‘, charset=‘utf8‘)
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        sql = "insert into classes (name) values (%s)"
        cursor.execute(sql, (classname,))
        conn.commit()

        return redirect("/classes/")                                 #重定向

## 删除
def del_class(request):

    id = request.GET.get(‘id‘)                                      # html拼接url,获取id方法
    import pymysql
    conn = pymysql.connect(host=‘127.0.0.1‘, user=‘root‘, password=‘123‘, db=‘s8day58‘, charset=‘utf8‘)
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    sql = "delete from classes where id=%s"
    cursor.execute(sql, (id,))
    conn.commit()

    return redirect(‘/classes/‘)

## 修改
def up_class(request):                                              #分流获取
    if request.method == ‘GET‘:                
        id = request.GET.get(‘id‘)
        import pymysql
        conn = pymysql.connect(host=‘127.0.0.1‘, user=‘root‘, password=‘123‘, db=‘s8day58‘, charset=‘utf8‘)
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        sql = "select * from classes where id=%s"
        cursor.execute(sql, (id,))
        classes = cursor.fetchone()
        print(classes)

        return render(request, "up_class.html", {"classid":classes[‘id‘], "classname":classes[‘name‘]})
        
                                                  #往html传id和name  1.传id是为了,返回服务器的时候知道改哪个    2.传name为了让输入框有要修改之前的数据
                                                  #html那边建立一个隐藏的输入框,将id存放在里面,输入框就相当一个容器
        
        
    else:                                                       
        classname = request.POST.get(‘classname‘)
        id        = request.POST.get(‘classid‘)
        print(classname, id)
        import pymysql
        conn = pymysql.connect(host=‘127.0.0.1‘, user=‘root‘, password=‘123‘, db=‘s8day58‘, charset=‘utf8‘)
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        sql = "update classes set name=%s where id=%s"
        cursor.execute(sql, (classname,id))
        conn.commit()

        return redirect(‘/classes/‘)

=======================模态框==================================
def modal_add_class(request):
    classname = request.POST.get(‘classname‘)
    print(classname)

    if not classname:
        return HttpResponse(‘nook‘)               #HttpResponse :浏览器走的http协议,所以可以往浏览器发数据
    else:
        ## 廉价数据库
        import pymysql
        conn = pymysql.connect(host=‘127.0.0.1‘, user=‘root‘, password=‘123‘, db=‘s8day58‘, charset=‘utf8‘)
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        sql = "insert into classes (name) values (%s)"
        cursor.execute(sql, (classname,))
        conn.commit()
        return HttpResponse(‘ok‘)




urlpatterns = [
 
    url(r‘^classes/‘, classes),
    url(r‘^add_class/‘, add_class),                #浏览器第一次跳转
    url(r‘^modal_add_class/‘, modal_add_class),
    url(r‘^del_class/‘, del_class),
    url(r‘^up_class/‘, up_class),
]

HTML主逻辑

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Title</title>
    <style>
 .shadow {
            background-color: black;
            height: 1000px;
            position: fixed;                                #定位
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            opacity: 0.4;                                    #透明度
            z-index: 999;
            display:none
        }
        .content{
            background-color: white;
            width:500px;
            height: 300px;
            position: fixed;
            top: 100px;
            left: 500px;
            right: 200px;
            z-index: 1000;                                    #内容层调到上面
            display:none                                    #隐藏在页面中
        }
        #error{
            color: red;
        }
    </style>

</head>
<body>
    <h2>班级列表</h2>
    <a href="/add_class/">增加班级</a>                                            #跳转到路由对应关系(a标签跳转的是get方式)
    <button id="btn">模态对话框添加班级</button>
    <table border="1px">
        <thead>
            <tr>
                <th>ID</th>
                <th>班级名称</th>
                <th>操作</th>            
            </tr>
        </thead>
        <tbody>
            {% for item in classes %}                                            #遍历添加列表行数
                <tr>
                    <td>{{ item.id }}</td>
                    <td>{{ item.name }}</td>
                    <td>
                        <a href="/del_class/?id={{ item.id }}">删除</a>|          #a标签将id数据传到后台的方法
                        <a href="/up_class/?id={{ item.id }}">更新</a>
                    </td>
                </tr>
            {% endfor %}
        </tbody>
    </table>

<div class="shadow"></div>                                                        #遮罩层

<div class="content">                                                            #内容层

        班级名: <input type="text" name="classname" id="classname">                #模态框局部刷新,必须用ajax
        <input type="button" value="提交" id="tijiao">
        <input type="button" id="cancel" value="取消">
        <span id="error"></span>

</div>

</body>
<script
  src="http://code.jquery.com/jquery-1.12.4.js"
  integrity="sha256-Qw82+bXyGq6MydymqBxNPYTaUXXq7c8v3CwiYwLLNXU="
  crossorigin="anonymous"></script>

<script>
    $(‘#btn‘).click(function () {                                                #显示模态框
        $(".shadow").show();
        $(".content").show();
    });
    $(‘#cancel‘).click(function () {                                            #隐藏模态框
        $(".shadow").hide();
        $(".content").hide();
    });

    $(‘#tijiao‘).click(function () {
        $.ajax({
            type: "POST",
            url : "/modal_add_class/",
            data:  {"classname":$(‘#classname‘).val()},                            #发往服务器的数据
            success: function (data) {                                            #data,服务器返回的数据
                if(data == ‘ok‘){
                    alert(‘添加成功!‘);                                            #弹出提示信息
                    window.location.href = ‘/classes/‘;                            #浏览器的重定向
                }else{
                    alert(‘添加失败!‘);
                    $(‘#error‘).text(data)                                        #往标签插入一条数据
                }

            }
        })
    });


</script>
</html>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Title</title>
</head>
<body>
<h2>添加班级</h2>
<form action="/add_class/" method="post">            输入框提交的方式是post
    班级名称: <input type="text" name="classname">    name=‘‘是url查询的唯一依据
    <input type="submit"  value="提交">
</form>

</body>
</html>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Title</title>
</head>
<body>
<h2>更新班级</h2>
<form action="/up_class/" method="post">
    <input type="hidden" name="classid" value="{{ classid }}">
    班级名称: <input type="text" name="classname" value="{{ classname }}">
    <input type="submit"  value="提交">
</form>

</body>
</html>

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消