当前位置:   article > 正文

Django--学生管理系统(django慢更)

Django--学生管理系统(django慢更)

在这里插入图片描述

START演示视频:

django新手

(登录 注册 增删查 已完成 简易)

一.登录功能、注册功能

1.没有注册账号,判断,提示该账户未注册,注册后写入数据库(mysql)
2.登录功能 --暂且提示登陆成功;空用户名提示、中文字符提示,不存在提示

登录模板层:


```python

```python

```html
<html>
<head>
    <title>测试登录</title>
    <style>
      form
            {
               width:100%;
                height:100%;
               margin-top: 0px;
				margin-bottom: 20px;
               background:#008B8B;
            }
       div
       {
        display:inline-block;
                padding-top: 255px;
                padding-bottom: 255px;
				padding-left: 1px;
				padding-right: 1px;

       }
    </style>
</head>
<body>
    <center>
<form method="post" action="/login_action/">
    <h1>登录测试</h1>
    <div>
    用户名:<input name="username", type="text"><br>
    密  码:<input name="password", type="password"><br>
    {{ error }}</br>
    <button id="btn", type="submit">登录</button>
    </div>

    {% csrf_token %}
</form>
</center>



</body>

</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

url

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/', views.index),
    path('sign/', views.sign),
    path('login/', views.login),
  • 1
  • 2
  • 3
  • 4
  • 5

视图层

def login(request):
    return render(request,'login.html',{'test':request.method})

def login_action(request):
    if request.method =='POST':
        user = request.POST.get('username')
        password = request.POST.get('password')


        if len(user)== 0 or len(password)==0:
            # response = HttpResponseRedirect('/login_right/')
            # response.set_cookie('user', user, 3600)
            # request.session['user'] = user
            # return response
            return render(request, 'login.html',{'error':'用户名密码不能为空'})

        else:

            for _char in user:
                if '\u4e00' <= _char <= '\u9fa5':
                    return render(request, 'sign.html',{'error':'用户名不能包含中文字符'})

            user_list = models.Admin.objects.all()
            logger.debug('开始判断用户:{}{}'.format(user,password))
            for judge_user in user_list:

                if judge_user.user == user and judge_user.password == password:
                    response = HttpResponseRedirect('/login_right/')
                    response.set_cookie('user', user, 3600)
                    models.Admin.objects.create(user = user,password=password)
                    request.session['user'] = user
                    return  response
                else:

                    continue

@login_required
def login_right(request):
    user =request.COOKIES.get('user','')
    # user =request.session.get('user','')

    return render(request,'login_action.html',{'user':user}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

模型层

class Student(models.Model):
    """
    创建如下几个表的字段
    """
    # 学号 primary_key=True: 该字段为主键
    studentNum = models.CharField('学号', primary_key=True, max_length=15)
    # 姓名 字符串 最大长度20
    name = models.CharField('姓名', max_length=20)
    # 年龄 整数 null=False, 表示该字段不能为空
    age = models.IntegerField('年龄', null=False)
    # 性别 布尔类型 默认True: 男生 False:女生
    sex = models.BooleanField('性别', default=True)
    # 手机 unique=True 该字段唯一
    mobile = models.CharField('手机', unique=True, max_length=15)
    # 创建时间 auto_now_add:只有在新增的时候才会生效
    createTime = models.DateTimeField(auto_now_add=True)
    # 修改时间 auto_now: 添加和修改都会改变时间
    modifyTime = models.DateTimeField(auto_now=True)
    hobby = models.CharField(max_length=13,default="study")

'''登录用户'''
class Admin(models.Model):
    user =models.CharField(max_length=20)
    password = models.CharField(max_length=20)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

二.登录成功 跳转主页 校验失败 提示

主页
在这里插入图片描述

在这里插入图片描述

三.增删改查-查询学生信息

templates:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>For Students</title>
</head>
<style>* {
        margin: 0px;
        padding: 0px;
    }

    .top {
        height: 100px;
        background-color: #041858;
        color: honeydew;
    }

    .banner {
        height: 41px;
        background-color: #fcfcfc;
        text-align: right;
        line-height: 50px;
        border-top: 3px solid #ff8500;
        border-bottom: solid 1px #edeef0;
    }

    .bc {
        display: inline-block;
        width: 80px;
        height: 40px;
        font-size: 20px;
        color: #4c4c4c;
        line-height: 40px;
        padding: 0px 20px;
        text-decoration: none;
    }
    a.x {
            display: block;
            width: 140px;
            height: 70px;
            background-color: rgba(124, 119, 124, 0.993);
            text-decoration: none;
            text-align: center;
            color: cornsilk;
            line-height: 70px;
        }

    a.x:hover {
        background-color: yellowgreen;
    }</style>
<body>
   <div class="header">
        <div class="top"><a style="text-decoration: none;line-height: 100px;font-family:STCaiyun; color: honeydew;font-size: 40px;" id="two">For Students</a></div>
        <div class="banner">
            <a class="bc" href="/index">首页导航</a>

            <a class="bc" href="yingxiaojieshao.html">学习园地</a>

            <a class="bc" href="anli.html">在校实习</a>

            <a class="bc" href="#">学生管理</a>

            <a class="bc" href="#">其他板块</a>
            <a class="bc" href="guanyu.html">关于网站</a>

        </div>
    </div>
   <center>
<p>学生表信息</p>
       <form action="/student_query" , method="get">
           <input name="student_query",type="text"><button id='btn',type="submit">查询</button>
           <br>
           {% for item in msg %}
           {{ item.studentNum }}{{ item.name }} {{ item.age }}

           {%  endfor %}
       </form>
<table border="1">
    <tr>
        <td>学号</td>
        <td>姓名</td>
        <td>年龄</td>
        <td>电话</td>
        <td>入学时间</td>
         <td>上次修改</td>
        <td>操作
        <a href="/student_add" > 添加</a></td>
    </tr>
    {% for item in student_list %}
    <tr>
         <td>{{ item.studentNum }}</td>
         <td>{{ item.name }}</td>
         <td>{{ item.age }}</td>
         <td>{{ item.mobile }}</td>
         <td>{{ item.createTime|date:"Y-m-d H:i:s" }}</td>
         <td>{{ item.modifyTime|date:"Y-m-d H:i:s" }}</td>
        <td>
         <a href="student_add" > 修改</a>
         <a href="/student_del/?studentNum={{ item.studentNum }}" > 删除</a></td>
    </tr>
    {% endfor %}
    {{ msg}}

</table>
       </center>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107

views

def student_info(request):
    student_list =models.Student.objects.all()

    # models.Admin.objects.filter(user='zhaowenyao').delete()
    return  render(request,'student_info.html',{'student_list':student_list})
  • 1
  • 2
  • 3
  • 4
  • 5

效果展示 点击 学生信息跳转
在这里插入图片描述

查询指定学生信息

views:

def student_query(request):
    if request.method == 'GET':
        studentNum =request.GET.get('student_query')
        data_list = models.Student.objects.filter(studentNum =studentNum)

        return render(request,'student_query.html',{'msg':data_list})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

模板

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>For Students</title>
</head>
<style>* {
        margin: 0px;
        padding: 0px;
    }

    .top {
        height: 100px;
        background-color: #041858;
        color: honeydew;
    }

    .banner {
        height: 41px;
        background-color: #fcfcfc;
        text-align: right;
        line-height: 50px;
        border-top: 3px solid #ff8500;
        border-bottom: solid 1px #edeef0;
    }

    .bc {
        display: inline-block;
        width: 80px;
        height: 40px;
        font-size: 20px;
        color: #4c4c4c;
        line-height: 40px;
        padding: 0px 20px;
        text-decoration: none;
    }
    a.x {
            display: block;
            width: 140px;
            height: 70px;
            background-color: rgba(124, 119, 124, 0.993);
            text-decoration: none;
            text-align: center;
            color: cornsilk;
            line-height: 70px;
        }

    a.x:hover {
        background-color: yellowgreen;
    }</style>
<body>
   <div class="header">
        <div class="top"><a style="text-decoration: none;line-height: 100px;font-family:STCaiyun; color: honeydew;font-size: 40px;" id="two">For Students</a></div>
        <div class="banner">
            <a class="bc" href="/index">首页导航</a>

            <a class="bc" href="yingxiaojieshao.html">学习园地</a>

            <a class="bc" href="anli.html">在校实习</a>

            <a class="bc" href="#">学生管理</a>

            <a class="bc" href="#">其他板块</a>
            <a class="bc" href="guanyu.html">关于网站</a>

        </div>
    </div>
   <center>
<p>学生表信息</p>
       <form action="/student_query" , method="get">
           <input name="student_query",type="text"><button id='btn',type="submit">查询</button> <a href="/student_info">返回</a>
           <br>
       </form>
<table border="1">
    <tr>
        <td>学号</td>
        <td>姓名</td>
        <td>年龄</td>
    </tr>
   {% for item in msg %}
    <tr>

          <td>{{ item.studentNum }}</td>
           <td>{{ item.name }}</td>
           <td> {{ item.age }}</td>

    </tr>
    {% endfor %}
     <br>
        {%if item.studentNum == None %}
         <p>学生信息不存在</p>

        {%endif%}

</table>
       </center>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98

删除学生信息

def student_del(request):
    if request.method == 'GET':
        studentNum = request.GET.get('studentNum')

        models.Student.objects.filter(studentNum=studentNum).delete()
        student_list = models.Student.objects.all()

        # models.Admin.objects.filter(user='zhaowenyao').delete()
        return render(request, 'student_info.html', {'student_list': student_list})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

增加学生信息

views

def student_add(request):
    if request.method == 'GET':
        return render(request,'student_add.html')
    if request.method == 'POST':
        name = request.POST.get('name')
        no = request.POST.get('no')
        age = request.POST.get('age')
        mobile = request.POST.get('mobile')
        logger.debug('开始判断用户:{}{}'.format(name, no))
        if len(name) == 0 or len(no) == 0 or len(age)==0 or len(mobile)==0:

            return render(request, 'student_add.html', {'error': '不能为空'})

        else:

            for _char in name:
                if '\u4e00' <= _char <= '\u9fa5':
                    return render(request, 'sign.html', {'error': '用户名不能包含中文字符'})
            models.Student.objects.create(studentNum=no, name=name, age=age, mobile=mobile)
            student_list = models.Student.objects.all()
            return   render(request, 'student_info.html', {'student_list':student_list,'msg': '添加成功'})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

模板

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生管理系统</title>
</head>
<style>* {
        margin: 0px;
        padding: 0px;
    }

    .top {
        height: 100px;
        background-color: #041858;
        color: honeydew;
    }

    .banner {
        height: 41px;
        background-color: #fcfcfc;
        text-align: right;
        line-height: 50px;
        border-top: 3px solid #ff8500;
        border-bottom: solid 1px #edeef0;
    }

    .bc {
        display: inline-block;
        width: 80px;
        height: 40px;
        font-size: 20px;
        color: #4c4c4c;
        line-height: 40px;
        padding: 0px 20px;
        text-decoration: none;
    }
    a.x {
            display: block;
            width: 140px;
            height: 70px;
            background-color: rgba(124, 119, 124, 0.993);
            text-decoration: none;
            text-align: center;
            color: cornsilk;
            line-height: 70px;
        }

    a.x:hover {
        background-color: yellowgreen;
    }</style>
<body>
 <div class="header">
        <div class="top"><a style="text-decoration: none;line-height: 100px;font-family:STCaiyun; color: honeydew;font-size: 40px;" id="two">学生管理系统</a></div>
        <div class="banner">
            <a class="bc" href="http://127.0.0.1:8000/student_info.html">首页导航</a>

            <a class="bc" href="yingxiaojieshao.html">学习园地</a>

            <a class="bc" href="anli.html">在校实习</a>

            <a class="bc" href="#">学生管理</a>

            <a class="bc" href="#">其他板块</a>
            <a class="bc" href="guanyu.html">关于网站</a>

        </div>
    </div>
<h2>添加学生表信息</h2>
<form method="post" action="/student_add/">
    <div>
    姓  名:<input name="name", type="text"><br>
    年  龄:<input name="age", type="text"><br>
    学  号:<input name="no", type="no"><br>
    电  话:<input name="mobile", type="mobile"><br>
    <button id="btn", type="submit">添加</button>
    {% csrf_token %}
    </div>
</form>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/154053
推荐阅读
相关标签
  

闽ICP备14008679号