专栏首页python3Django之学员管理一

Django之学员管理一

Django之学员管理一

建表结构:

#班级表class:
id    title
1    五年一班
2    五年二班
3    五年三班
4    五年四班

#学生表student:
id    name          班级ID(FK外键)
1    George        1
2    Jack            2
3    Tom            2
4    Alex            1

#老师表teacher:
id    name
1    Crystal
2    Lee
3    Iron Man

#老师班级关系表teacher_and_class:
id    老师ID    班级ID
1        1            1
2        1            2
3        2            3
4        2            4
5        3            1
6        3            2

操作单表:班级表和老师表

views.py

from django.shortcuts import render,redirect
import pymysql

def classes(request):
    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='redhat', db='学员管理',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,'classes.html',{'class_list': class_list})
 
def add_class(request):
    if request.method == "GET":
        return render(request,'add_class.html')
    else:
        print(request.POST)
        v = request.POST.get('title')
        conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='redhat', db='学员管理',charset='utf8')
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        cursor.execute("insert into class(title) values(%s)",[v,])
        conn.commit()
        cursor.close()
        conn.close()
        return redirect('/classes/')
 
def del_class(request):
    nid = request.GET.get('nid')
    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='redhat', db='学员管理',charset='utf8')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cursor.execute("delete from class where id=%s",[nid,])
    conn.commit()
    cursor.close()
    conn.close()
    return redirect('/classes/')
 
def edit_class(request):
    if request.method == "GET":
        nid = request.GET.get('nid')
        conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='redhat', db='学员管理',charset='utf8')
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        cursor.execute("select id,title from class where id = %s",[nid,])
        result = cursor.fetchone()
        cursor.close()
        conn.close()
        print(result)
        return render(request,'edit_class.html',{'result':result})
    else:
        nid = request.GET.get('nid')
        title = request.POST.get('title')
        conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='redhat', db='学员管理',charset='utf8')
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        cursor.execute("update class set title=%s where id = %s",[title,nid,])
        conn.commit()
        cursor.close()
        conn.close()
        return redirect('/classes/')

 classes.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <h1>班级列表</h1>
    <div>
        <a href="/add_class/">添加</a>
    </div>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>班级名称</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {% for row in class_list %}
                <tr>
                    <td>{{ row.id }}</td>
                    <td>{{ row.title }}</td>
                    <td>
                        <a href="/edit_class/?nid={{ row.id }}">编辑</a>
                        |
                        <a href="/del_class/?nid={{ row.id }}">删除</a>
                    </td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
</body>
</html>

add_class.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <h1>添加班级</h1>
    <form method="POST" action="/add_class/">
        <p>班级名称:<input type="text" name="title" /></p>
        <input type="submit" value="提交" />
    </form>
</body>
</html>

edit_class.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <h1>编辑班级</h1>

    <form method="POST" action="/edit_class/?nid={{ result.id }}">
        <p>班级名称:<input type="text" name="title" value="{{ result.title }}" /></p>
        <input type="submit" value="提交" />
    </form>
</body>
</html>

 url.py

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^classes/', views.classes),
    url(r'^add_class/', views.add_class),
    url(r'^del_class/', views.del_class),
    url(r'^edit_class/', views.edit_class),
]

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • python3中多重继承的问题

    本来以为多重继承很简单,但是多看了一些资料后发现还是挺复杂的。 如果继承情况简单就还比较好理解,但是如果继承的情况太过于复杂的话,python3 ...

    py3study
  • python 监控mysql脚本

    py3study
  • python记录day_20 多继承

    在python中,支持多继承,一个类可以拥有多个父类。但是多继承中, 存在着这样一个问题,当两个父类中出现了重名方法的时候该怎么办呢? 这时就涉及到如何查找父类...

    py3study
  • tf.estimator

    于小勇
  • Objective-C的hook方案/ Method Swizzling

    Method Swizzling是改变一个selector的实际实现的技术。通过这一技术,我们可以在运行时通过修改类的分发表中selector对应的函数,来修改...

    freesan44
  • Jmockdata随机模拟 Java 数据插件

         Jmockdta是一款实现模拟JAVA类型或对象的实例化并随机初始化对象的数据的工具框架。

    wuweixiang
  • Java .class文件是什么?

    java class 文件是对Java程序二进制文件格式的精确定义。每一个Java class文件都对一个Java类或者Java接口做出了全面描述。一个cla...

    MickyInvQ
  • iOS运行时(4)——常用函数

    羊羽shine
  • SpringBoot整合多数据源的巨坑

    爱撒谎的男孩
  • (1)Objective-C的本质

    czjwarrior

扫码关注云+社区

领取腾讯云代金券