day58

day58    做一个小型的学员管理系统
##刷新页面,到一个页面是GET,提交什么的是post,a标签跳转是get方式
render 把py里的东西传到html文件里
window.confirm('是否确认删除');
#新url的方式
###更新老师等
##urls.py的内容
from django.conf.urls import url
from django.contrib import admin
from django.shortcuts import HttpResponse,render,redirect


def f3(sql,*args):
    import pymysql
    conn = pymysql.connect(host='192.168.110.34', user='root', password='123456', db='new', charset='utf8')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    # sql = "select id,name,password from userinfo"
    cursor.execute(sql)
    res = cursor.fetchall()
    return res


def f4(sql,*args):
    import pymysql
    conn = pymysql.connect(host='192.168.110.34', user='root', password='123456', db='new', charset='utf8')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    # sql = "select id,name,password from userinfo"
    cursor.execute(sql,(args[0],))
    res = cursor.fetchone()
    return res


def change_sth(sql,*args):
    import pymysql
    conn = pymysql.connect(host='192.168.110.34', user='root', password='123456', db='new', charset='utf8')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    # sql = "select id,name,password from userinfo"
    cursor.execute(sql,(args[0],))
    conn.commit()
    #res = cursor.fetchall()


def change_sths(sql,*args):
    import pymysql
    conn = pymysql.connect(host='192.168.110.34', user='root', password='123456', db='new', charset='utf8')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    # sql = "select id,name,password from userinfo"
    cursor.execute(sql,(args[0],args[1],))
    conn.commit()




def classes(request):
    sql = "select * from classes"
    classes = f3(sql)
    print(classes)
    return render(request, "classes.html", {'classes':classes})


def teacher(request):
    sql="select * from teacher"
    teachers = f3(sql)
    return render(request,"teacher.html",{'teacher':teachers})


def add_teacher(request):
    # print('-----',request)
    if request.method == 'GET':
        return render(request,'add_teacher.html')
    else:
        ##这个老师的名字是html提交过来的
        teachername=request.POST.get('teachername')
        sql = "insert into teacher (name) values (%s)"
        change_sth(sql,teachername)
        return redirect("/teacher/")
        ###print(teachername)
def del_teacher(request):
    #让用户在页面点击删除的同时,把id号传过来.用id号进行删除
    id = request.GET.get('id')
    sql="delete from teacher where id=%s"
    change_sth(sql,id)
    return redirect("/teacher/")


def up_teacher(request):
    ###id是从teacher_id 传过来的
    if request.method == 'GET':   #判断如果是到这个页面(刷新这个页面),则是GET.这个界面的提交就是POST
        id=request.GET.get('id')
        print(id)
        sql="select * from teacher where id=%s"
        teacher=f4(sql,id)
        print('----',teacher)
        return render(request,"up_teacher.html",{"teacherid":teacher['id'],"teachername":teacher['name']})      #跳转到html页面并且把id和name提交给up_teacher.html
    else:
        #从别人前端页面输入的id得到name和id,然后对id和name进行数据库的更改
        teachername = request.POST.get('teachername')
        teacherid = request.POST.get('teacherid')
        sql="update teacher set name=%s where id =%s"
        change_sths(sql,teachername,teacherid)
        return redirect('/teacher/')


urlpatterns = [
    #url(r'^admin/', admin.site.urls),
    url(r'^classes/', classes),
    url(r'^teacher/', teacher),
    url(r'^add_teacher/', add_teacher),
    url(r'^del_teacher/', del_teacher),
    url(r'^up_teacher/', up_teacher),
]

#teacher.html

 

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h2>老师列表</h2>
    <a href="/add_teacher/">增加老师</a>
    <table border="1px">
        <thead>
        <tr>
            <th>ID</th>
            <th>教师名</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
            {% for item in teacher %}
                <tr>
                    <td>{{ item.id }}</td>
                    <td>{{ item.name }}</td>
                    <td>
                        <a href="/del_teacher/?id={{ item.id }}">删除</a>|||<a href="/up_teacher/?id={{ item.id }}">更新</a>     {# 点击更新按钮的时候这个页面把id传到up_teacher #}
                    </td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
</body>
</html>

 

 ###add_teacher.html
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>


</head>
<body>
    <h2>添加老师</h2>
    <form action="/add_teacher/" method="post">
        {# 当输入这个老师名称的时候输的名字就等于teachername,这个就等于是个变量       #}
        老师名称:<input type="text" name="teachername">
        <input type="submit" value="提交">
    </form>
</body>
</html>

 #up_teacher.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        h2 {
            color: red;
        }
    </style>
</head>
<body>
    <h2>更新教师名</h2>
    <form action="/up_teacher/" method="post">   {# 点更新按钮是post请求 #}
        <input type="hidden" name="teacherid" value={{ teacherid }}>     {# 需要从get传id进来,但是不需要让用户看到所以隐藏,然后传到post请求里去 #}
       教师名:  <input type="text" name="teachername" value={{ teachername }}>    {# 需要将老师的名字显示在框里面,然后他的这个输入的name是需要传到post请求里去 #}
        <input type="submit" value="更新">
    </form>
</body>
</html>

 ###模态对话框的形式

 

 

posted @ 2019-07-10 20:04  轩辕12  阅读(193)  评论(0编辑  收藏  举报