因为是边学边做的,所以有一些地方有了很麻烦的方法,登录验证也没做完,前端也很难看,只是做了基本的增删改查
1.新建project
django-admin.py startproject student
2.新建app
python manage. py startapp guanli
3.setting设置
在
INSTALLED_APPS =
中添加新建的guanli app
4.urls设置
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('guanli/', include(('guanli.urls','guanli'), namespace='guanli')) ]
5.进入guanli app 下的models中添加数据库表
from django.db import models class student(models.Model): xuehao = models.IntegerField(primary_key=True, verbose_name='学号') xingming = models.TextField(blank=True, null=True, verbose_name='姓名') xingbie = models.TextField(blank=True, null=True) age = models.IntegerField(blank=True, null=True, verbose_name='年龄') csrq = models.DateField(blank=True, null=True) bjbh = models.IntegerField(blank=True, null=True) phone = models.TextField(blank=True, null=True) sfzh = models.IntegerField(blank=True, null=True) def __int__(self): return self.xuehao class teacher(models.Model): bianhao = models.IntegerField(primary_key=True) xingming = models.CharField(max_length=32) xingbie = models.TextField() csrq = models.DateField() zzmm = models.TextField() phone = models.IntegerField() sfzh = models.IntegerField() class login(models.Model): user = models.TextField() password = models.TextField()5.使用
1 python manage.py makemigrations 2 python manage.py migrate
将数据库信息添加到sqlite3中,因为是内置的所以不需要设置api
6.在guanli下的admin中添加
from django.contrib import admin from .models import student from .models import teacher from .models import login class ss(admin.ModelAdmin): list_display = ('xuehao', 'xingming', 'age') # 页面显示 # list_editable = ('xingming','age') #直接修改 # filter_horizontal = ('age',) # list_per_page = 2 #分页 # search_fields = ('xuehao','xingming') #查询 # list_filter = ('age','xuehao') #辅助过滤查询 admin.site.register(student, ss) admin.site.register(teacher) admin.site.register(login)
在guanli 的apps中
from django.apps import AppConfig class GuanliConfig(AppConfig): name = 'guanli'
7.在guanlixia的urls中
from django.urls import path from. import views app_name='guanli' urlpatterns = [ path('index/', views.index), path('sms/', views.stus), path('gon/', views.gon,name='gon'), path('add/', views.add,name='add'), path('query/', views.query,name='query'), path('change/', views.change,name='change'), path('delete/', views.delete,name='delete'), path('cxjg/', views.cxjg,name='cxjg'), path('change1/', views.change1,name='change1'), path('xiugai/', views.xiugai,name='xiugai'), path('shanchu/', views.shanchu,name='shanchu'), path('login/', views.login,name='login'), path('dele/', views.dele,name='dele'), path('logi/', views.logi,name='logi'), ]8.在guanli 下的views中
from django.shortcuts import render from django.http import HttpResponse from .import models def index(request): return render(request,'ss/index.html') def stus(request): stus=models.student.objects.all() return render(request,'ss/stus.html',{'stus':stus}) def gon(request): return render(request,'ss/gon.html') def add(request): xh = request.POST.get('xuehao') xm = request.POST.get('xingming') xingbie = request.POST.get('xingbie') age = request.POST.get('age') csrq = request.POST.get('csrq') bjbh = request.POST.get('bjbh') phone = request.POST.get('phone') sfzh = request.POST.get('sfzh') # bc = models.student() # bc.xuehao = xh # bc.xingming=xm # bc.xingbie=xingbie # bc.age=age # bc.csrq=csrq # bc.bjbh=bjbh # bc.phone=phone # bc.sfzh=sfzh # bc.save() try: if (xm): models.student.objects.create(xuehao=xh, xingming=xm, xingbie=xingbie, age=age, csrq=csrq, bjbh=bjbh, phone=phone, sfzh=sfzh) return render(request, 'ss/add.html') except: aa='aa' return render(request, 'ss/add.html',{'aa':aa}) def query(request): return render(request,'ss/query.html') def cxjg(request): try: xh=int(request.POST.get('xuehao')) xinxi=models.student.objects.get(pk=xh) quan = models.student.objects.all() if (xinxi in quan): return render(request,'ss/query.html',{'aa':xinxi}) else: return render(request,'ss/query.html') except: bcz='该学号不存在' return render(request,'ss/query.html',{'bcz':bcz}) def change(request): xh = request.POST.get('xuehao') xinxi = models.student.objects.get(xuehao=xh) return render(request,'ss/change.html',{'bb':xinxi}) def xiugai(request): return render(request,'ss/xiugai.html') def change1(request): xh = request.POST.get('xuehao1') obj = models.student.objects.get(xuehao=xh) obj.xuehao = request.POST.get('xuehao1') obj.xingming = request.POST.get('xingming1') obj.xingbie = request.POST.get('xingbie1') obj.age = request.POST.get('age1') obj.csrq = request.POST.get('csrq1') obj.bjbh = request.POST.get('bjbh1') obj.phone = request.POST.get('phone1') obj.sfzh = request.POST.get('sfzh1') obj.save() return render(request,'ss/gon.html') def delete(request): return render(request, 'ss/delete.html') def dele(request): try: xh=int(request.POST.get('xuehao')) xinxi=models.student.objects.get(pk=xh) quan = models.student.objects.all() if (xinxi in quan): return render(request,'ss/delete.html',{'aa':xinxi}) else: return render(request,'ss/delete.html') except: bcz='该学号不存在' return render(request,'ss/delete.html',{'bcz':bcz}) def shanchu(request): xh = request.POST.get('xuehao') models.student.objects.get(xuehao=xh).delete() return HttpResponse('删除成功') def logi(request): user1 = request.POST.get('user') password = request.POST.get('password') users=models.login.objects.all() err='用户名或密码错误' for user in users: if user.user == user1 and user.password == password: return render(request, 'ss/index.html') return render(request, 'ss/login.html',{'err':err}) def login(request): return render(request, 'ss/login.html')
在guanli下新建一个文件夹 Templates,在Templates中新建文件夹ss,在ss下新建html文件
1.add.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>增加</title> <script> function OK(){alert("提交成功");} function NO(){alert("该学号已存在");} </script> </head> <body> <h6><a href="{% url 'guanli:gon' %}"><<< 返回管理页面</a></h6> <div style="width:100%;text-align:center"> <form action="{% url 'guanli:add' %}" method="post"> {% csrf_token %} <label>学号 <input type="text" name="xuehao" /> </label> <br/> <label>姓名 <input type="text" name="xingming" /> </label> <br/> <label>性别 男<input type="radio" name="xingbie" value="男"/>女<input type="radio" name="xingbie" value="女"/> </label> <br/> <label>年龄 <input type="text" name="age" /> </label> <br/> <label>出生日期 <input type="date" name="csrq" value="2018-01-01" onfocus="if (value =='2018-01-01'){value =''}"onblur="if (value ==''){value='2018-01-01'}" /> </label> <br/> <label>班级编号 <input type="text" name="bjbh" value="1" onfocus="if (value =='1'){value =''}"onblur="if (value ==''){value='1'}" /> </label> <br/> <label>电话号码 <input type="text" name="phone" /> </label> <br/> <label>身份证号 <input type="text" name="sfzh" /> </label> <br/> {%if aa%} <input type="submit" value="确认" onclick="NO()"> {%else%} <input type="submit" value="确认" onclick="OK()"> {%endif%} </form> </div> </body> </html>
2. change.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>更改</title> </head> <body> <h6><a href="{% url 'guanli:gon' %}"><<< 返回管理页面</a></h6> <div style="width:100%;text-align:center"> <form action="{% url 'guanli:change1' %}" method="post"> {% csrf_token %} <label>学号 <input type="text" name="xuehao1" value="{{ bb.xuehao }}"/> </label> <br/> <label>姓名 <input type="text" name="xingming1" value="{{ bb.xingming }}"/> </label> <br/> <label>性别 <input type="text" name="xingbie1" value="{{ bb.xingbie }}"/> </label> <br/> <label>年龄 <input type="text" name="age1" value="{{ bb.age }}"/> </label> <br/> <label>出生日期 <input type="text" name="csrq1" value="{{ bb.csrq }}"/> </label> <br/> <label>班级编号 <input type="text" name="bjbh1" value="{{ bb.bjbh }}"/> </label> <br/> <label>电话号码 <input type="text" name="phone1" value="{{ bb.phone }}"/> </label> <br/> <label>身份证号 <input type="text" name="sfzh1" value="{{ bb.sfzh }}"/> </label> <br/> <input type="submit" value="确认修改"> </form> </div> </body> </html>
3.csjg.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>11</title> </head> <body> <h6><a href="{% url 'guanli:gon' %}"><<< 返回管理页面</a></h6> <p>{{aa}}</p> </body> </html>
4.delete.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>删除</title> </head> <h6><a href="{% url 'guanli:gon' %}"><<< 返回管理页面</a></h6> <body> <div style="width:100%;text-align:center"> <h2>请输入要删除的学生学号</h2> <form action="{% url 'guanli:dele' %}" method="post"> {% csrf_token %} <label>学号 <input type="text" name="xuehao" value="{{ student.xuehao }}"/> </label> <br/> <input type="submit" value="确认"> </form> <h4>{{bcz}}</h4> {%if aa%} <form action="{% url 'guanli:shanchu' %}" method="post"> {% csrf_token %} <label>学号 <input type="text" name="xuehao" value="{{aa.xuehao}}"/> </label> <br/> <label>姓名 <input type="text" name="xingming" value="{{aa.xingming}}"/> </label> <br/> <label>性别 <input type="text" name="xingbie" value="{{aa.xingbie}}"/> </label> <br/> <label>出生日期 <input type="text" name="csrq" value="{{aa.csrq}}"/> </label> <br/> <label>班级编号 <input type="text" name="bjbh" value="{{aa.bjbh}}"/> </label> <br/> <label>电话号码 <input type="text" name="phone" value="{{aa.phone}}"/> </label> <br/> <label>身份证号 <input type="text" name="sfzh" value="{{aa.sfzh}}"/> </label> <br/> <input type="submit" value="确认删除"> {%endif%} </div> </body> </html>
5.gon.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div style="background-color:#84C1FF;width:100%;height:50px;line-height:50px;top:0;text-align:center;"> <h2>学生管理系统</h2> </div> <div style="width:100%;text-align:center"> <h3><a href="{% url 'guanli:query' %}">查询信息</a></h3> <h3><a href="{% url 'guanli:xiugai' %}">更改信息</a></h3> <h3><a href="{% url 'guanli:delete' %}">删除信息</a></h3> <h3><a href="{% url 'guanli:add' %}">增加信息</a></h3> </div> </body> </html>
6.index.html
<!DOCTYPE html> {% load staticfiles %} <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> a{ text-decoration: none } .abc{ color: white; background-color:#84C1FF;width:100%;height:50px;line-height:10px;text-align:center; position:absolute;top: 0;left: 0; } .cba{width:100%;text-align:center; position:absolute;top: 70px} </style> </head> <body> <div class='abc'> <h2>学生管理系统</h2> </div> <div class="cba"> <h3> <a href="{% url 'guanli:gon' %}">学生管理</a></h3> <h3> <a href="">教师管理</a></h3> </div> </body> </html>
7.login.html
<!DOCTYPE html> {% load staticfiles %} <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> #bg{ background:url(/static/1.jpg) no-repeat;text-align:center;margin-left:auto;margin-right:auto; position:fixed; top: 0; left: 0; width:100%; height:100%; min-width: 1000px; z-index:-10; zoom: 1; background-size: cover; -webkit-background-size: cover; -o-background-size: cover; background-position: center 0; } #gg{ background-color:rgba(255,255,255,0.6);width:300px;text-align:center; position:absolute;top: 100px;left: 50%; } </style> </head> <body><div id=bg> <div id=gg> <h2>账号登录</h2> <form action="{% url 'guanli:logi' %}" method="post"> {% csrf_token %} <label>用户名: <input type="text" name="user" placeholder="用户名"/> </label> <br/> <label>密码: <input type="password" name="password" placeholder="密码"/> </label> <br/> <input type="submit" value="登录"> <a>{{err}}</a> </form> </div> </div> </body> </html>
8.query.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>查询</title> </head> <body> <h6><a href="{% url 'guanli:gon' %}"><<< 返回管理页面 </a></h6> <div style="width:100%;text-align:center"> <h2>请输入要查询的学生学号</h2> <form action="{% url 'guanli:cxjg' %}" method="post"> {% csrf_token %} <label>学号 <input type="text" name="xuehao" value=""/> </label> <br/> <input type="submit" value="确认"> </form> <p> {{bcz}}</p> {%if aa%} <form action="{% url 'guanli:change' %}" method="post"> {% csrf_token %} <label>学号 <input type="text" name="xuehao" value="{{aa.xuehao}}"/> </label> <br/> <label>姓名 <input type="text" name="xingming" value="{{aa.xingming}}"/> </label> <br/> <label>性别 <input type="text" name="xingbie" value="{{aa.xingbie}}"/> </label> <br/> <label>年龄 <input type="text" name="age" value="{{aa.age}}"/> </label> <br/> <label>出生日期 <input type="text" name="csrq" value="{{aa.csrq}}"/> </label> <br/> <label>班级编号 <input type="text" name="bjbh" value="{{aa.bjbh}}"/> </label> <br/> <label>电话号码 <input type="text" name="phone" value="{{aa.phone}}"/> </label> <br/> <label>身份证号 <input type="text" name="sfzh" value="{{aa.sfzh}}"/> </label> <br/> <input type="submit" value="修改信息"> {%endif%} </form> </div> </body> </html>
9.stus.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>学生管理系统</title> </head> <body> {% for i in stus %} <h1>学号:{{i.xuehao}} 姓名:{{i.xingming}}</h1> {% endfor %} </body> </html>
10.xiugai.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h6><a href="{% url 'guanli:gon' %}"> <<< 返回管理页面 </a></h6> <div style="width:100%;text-align:center"> <h2>请输入要更改的学生学号</h2> <form action="{% url 'guanli:change' %}" method="post"> {% csrf_token %} <label>学号 <input type="text" name="xuehao" /> </label> <br/> <input type="submit" value="确认"> </form> </div> </body> </html>
添加静态文件,在student的最后加上
STATIC_URL = '/static/' STATICFILES_DIRS = [(os.path.join(BASE_DIR, 'static'))]
在guanli下新建static文件夹,存放