python3.6+django2.0開發(fā)一套學員管理系統(tǒng)
1.在pycharm中新建project demo1 添加app01 點擊create按鈕完成新建
2.在demo項目目錄下新建目錄static,并在settings.py中追加代碼:
STATICFILES_DIRS=(os.path.join(BASE_DIR, 'static'),)
3.在setting.py中添加模板路徑:
TEMPLATES = [
{
'BACKEND': '...',
'DIRS': [os.path.join(BASE_DIR, 'templates'),],
'APP_DIRS': ...,
'OPTIONS': {
'context_processors': [
...
],
},
},
]
4.學員管理系統(tǒng)數(shù)據(jù)庫設計:
在app01/model.py目錄下建立 班級、老師、學生 、老師與班級關聯(lián)表 四張表:
from django.db import models
# Create your models here.
class Classes(models.Model):
'''
班級表
'''
title=models.CharField(max_length=32)
a=models.ManyToManyField('Teachers')
class Teachers(models.Model):
'''
老師表
'''
name=models.CharField(max_length=32)
class Students(models.Model):
username=models.CharField(max_length=32)
age=models.IntegerField()
gender=models.BooleanField()
cs=models.ForeignKey(Classes,on_delete=models.CASCADE)
在終端Terminal 項目目錄下執(zhí)行數(shù)據(jù)表更新命令:
python manage.py makemigrations python manage.py migrate
至此生成了四張數(shù)據(jù)表,可以在pycharm中,點開右上角的Database面板,然后將項目中templates目錄下邊的db.sqlite3鼠標拖拽到Database面板下,對新創(chuàng)建的數(shù)據(jù)表進行查看。
5.學員管理系統(tǒng)之班級管理:
為了方便分別操作班級、老師、學生相關的業(yè)務,將app01目錄下的views.py 刪掉,在app01目錄下新建目錄views,并在views目錄下 新建classes.py teachers.py students.py。
1.在classes.py 中寫 get_classes add_classes del_classes edit_classes四個函數(shù),完成對 班級數(shù)據(jù) 的增刪改查:
from django.shortcuts import render,redirect
from app01 import models
def get_classes(request):
cls_list = models.Classes.objects.all()
return render(request,'get_classes.html',{'cls_list':cls_list})
def add_classes(request):
if request.method=='GET':
return render(request,'add_classes.html')
elif request.method=='POST':
title=request.POST.get('title','')
models.Classes.objects.create(title=title)
return redirect('/classes.html')
def del_classes(request):
nid=request.GET.get('nid','')
models.Classes.objects.filter(id=nid).delete()
return redirect('/classes.html')
def edit_classes(request):
if request.method=="GET":
nid = request.GET.get('nid', '')
obj=models.Classes.objects.get(id=nid)
return render(request,'edit_classes.html',{'obj':obj})
elif request.method=="POST":
nid=request.POST.get('nid','')
title=request.POST.get('xxoo','')
models.Classes.objects.filter(id=nid).update(title=title)
return redirect('/classes.html')
2.在urls.py 中配置url路由:
from django.contrib import admin
from django.urls import path
from app01.views import classes,students,teachers
urlpatterns = [
path('admin/', admin.site.urls),
path('classes.html', classes.get_classes),
path('add_classes.html', classes.add_classes),
path('del_classes.html', classes.del_classes),
path('edit_classes.html', classes.edit_classes),
# path('teachers.html', teachers.get_teachers),
# path('students.html', students.get_studernts),
]
3.在template目錄下建立所需的html頁面文件:
get_classes.html
DOCTYPE html>
<html lang="en">
<head>
<style>
tr td{ border:1px solid #000;text-align:center;}
</style>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<table>
<thead>
<tr>
<th>ID</th> <th>名稱</th> <th>操作</th>
</tr>
</thead>
<tbody>
{% for row in cls_list %}
<tr>
<td>{{ row.id }}</td>
<td>{{ row.title }}</td>
<td><a href="/del_classes.html?nid={{ row.id }}" rel="external nofollow" rel="external nofollow" >刪除</a>
|<a href="/edit_classes.html?nid={{ row.id }}" rel="external nofollow" rel="external nofollow" >編輯</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div><a href="/add_classes.html" rel="external nofollow" rel="external nofollow" >添加</a> </div>
</body>
</html>
add_classes.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/add_classes.html" method="post">
{% csrf_token %}
<input type="text" name="title">
<input type="submit" value="提交">
</form>
</body>
</html>
edit_classes.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="/edit_classes.html">
{% csrf_token %}
<input type="hidden" name="nid" value="{{ obj.id }}">
<input type="text" name="xxoo" value="{{ obj.title }}">
<input type="submit" value="提交">
</form>
</body>
</html>
6.學員管理系統(tǒng)之學員管理: 1.在students.py 中寫 get_students add_students del_students edit_students 四個函數(shù),完成對 學生數(shù)據(jù) 的增刪改查:
from django.shortcuts import render,redirect
from app01 import models
def get_students(request):
stu_list=models.Students.objects.all()
return render(request,'get_students.html',{'stu_list':stu_list})
def add_students(request):
if request.method=='GET':
cs_list=models.Classes.objects.all()
return render(request,'add_students.html',{'cs_list':cs_list})
elif request.method=='POST':
u=request.POST.get('username','')
a=request.POST.get('age','')
g=request.POST.get('gender','')
c=request.POST.get('cs','')
models.Students.objects.create(
username=u,
age=a,
gender=g,
cs_id=c
)
return redirect('/students.html')
def del_students(request):
nid = request.GET.get('nid', '')
models.Students.objects.filter(id=nid).delete()
return redirect('/students.html')
def edit_students(request):
if request.method=="GET":
nid = request.GET.get('nid', '')
obj=models.Students.objects.get(id=nid)
cs_list = models.Classes.objects.all()
return render(request,'edit_students.html',{'obj':obj,'cs_list':cs_list})
elif request.method=="POST":
nid=request.POST.get('nid','')
u = request.POST.get('username', '')
a = request.POST.get('age', '')
g = request.POST.get('gender', '')
c = request.POST.get('cs', '')
models.Students.objects.filter(id=nid).update(
username=u,
age=a,
gender=g,
cs_id=c)
return redirect('/students.html')
2.在urls.py 中配置url路由:
from django.contrib import admin
from django.urls import path
from app01.views import classes,students,teachers
urlpatterns = [
path('admin/', admin.site.urls),
path('classes.html', classes.get_classes),
path('add_classes.html', classes.add_classes),
path('del_classes.html', classes.del_classes),
path('edit_classes.html', classes.edit_classes),
path('students.html', students.get_students),
path('add_students.html', students.add_students),
path('del_students.html', students.del_students),
path('edit_students.html', students.edit_students),
# path('teachers.html', teachers.get_teachers),
]
3.在template目錄下建立所需的html頁面文件:
get_students.html
<!DOCTYPE html>
<html lang="en">
<head>
<style>
tr td{ border:1px solid #000;text-align:center;}
</style>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<table>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年齡</th>
<th>性別</th>
<th>班級</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for row in stu_list %}
<tr>
<td>{{ row.id }}</td>
<td>{{ row.username }}</td>
<td>{{ row.age }}</td>
<td>{{ row.gender }}</td>
<td>{{ row.cs.title }}</td>
<td><a href="/del_students.html?nid={{ row.id }}" rel="external nofollow" rel="external nofollow" >刪除</a>
|<a href="/edit_students.html?nid={{ row.id }}" rel="external nofollow" rel="external nofollow" >編輯</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div><a href="/add_students.html" rel="external nofollow" rel="external nofollow" >添加</a> </div>
</body>
</html>
add_students
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>添加用戶</h1>
<form method="post" action="/add_students.html">
{% csrf_token %}
<p><input type="text" name="username" placeholder="用戶名"></p>
<p><input type="text" name="age" placeholder="年齡"></p>
<p>
男<input type="radio" name="gender" value="1">
女<input type="radio" name="gender" value="0">
</p>
<p>
<select name="cs">
{% for row in cs_list %}
<option value="{{ row.id }}">{{ row.title }}</option>
{% endfor %}
</select>
</p>
<p><input type="submit" value="提交"></p>
</form>
</body>
</html>
edit_students.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>編輯用戶</h1>
<form method="post" action="/edit_students.html">
{% csrf_token %}
<input type="hidden" name="nid" value="{{ obj.id }}">
<p><input type="text" name="username" placeholder="用戶名"></p>
<p><input type="text" name="age" placeholder="年齡"></p>
<p>
男<input type="radio" name="gender" value="1">
女<input type="radio" name="gender" value="0">
</p>
<p>
<select name="cs">
{% for row in cs_list %}
<option value="{{ row.id }}">{{ row.title }}</option>
{% endfor %}
</select>
</p>
<p><input type="submit" value="提交"></p>
</form>
</body>
</html>
7.學員管理系統(tǒng)之給班級分配老師:
在teachers數(shù)據(jù)表中增加一些老師信息:
在pycharm右上角的Database打開面板,然后將template目錄下邊的db.splte3鼠標拖入到Database面板中,打開db==》app01_teachers表
點擊“+”,然后填入老師信息,然后點擊有“DB”標志的向上箭頭,進行數(shù)據(jù)保存。
1.在classes.py中增加set_teachers函數(shù)
def set_teachers(request):
if request.method=='GET':
nid=request.GET.get('nid','')
cls_obj=models.Classes.objects.get(id=nid)
cls_teacher_list=cls_obj.a.all()
all_teacher_list=models.Teachers.objects.all()
return render(request,'set_teachers.html',{
'cls_teacher_list':cls_teacher_list,
'all_teacher_list':all_teacher_list,
'nid':nid,
})
elif request.method=='POST':
nid = request.POST.get('nid', '')
ids_str=request.POST.getlist('teacher_id','')
ids_int=[]
for i in ids_str:
i=int(i)
ids_int.append(i)
obj=models.Classes.objects.get(id=nid)
obj.a.set(ids_int)
return redirect('/classes.html')
2.在urls.py 中配置url路由:
from django.contrib import admin
from django.urls import path
from app01.views import classes,students,teachers
urlpatterns = [
path('admin/', admin.site.urls),
path('classes.html', classes.get_classes),
path('add_classes.html', classes.add_classes),
path('del_classes.html', classes.del_classes),
path('edit_classes.html', classes.edit_classes),
path('students.html', students.get_students),
path('add_students.html', students.add_students),
path('del_students.html', students.del_students),
path('edit_students.html', students.edit_students),
path('set_teachers.html', classes.set_teachers),
]
3.在template目錄下建立所需的html頁面文件:
set_teachers.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/set_teachers.html" method="post">
<input type="hidden" name="nid" value="{{ nid }}">
{% csrf_token %}
<select multiple size="10" name="teacher_id">
{% for item in all_teacher_list %}
{% if item in cls_teacher_list %}
<option value="{{ item.id }}" selected="selected">{{ item.name }}</option>
{% else %}
<option value="{{ item.id }}">{{ item.name }}</option>
{% endif %}
{% endfor %}
</select>
<input type="submit" value="提交">
</form>
</body>
</html>
對get_classes.html進行增添修改為:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<table>
<thead>
<tr>
<th>ID</th> <th>名稱</th> <th>任課老師</th> <th>操作</th>
</tr>
</thead>
<tbody>
{% for row in cls_list %}
<tr>
<td>{{ row.id }}</td>
<td>{{ row.title }}</td>
<td>
{% for item in row.a.all %}
<span>{{ item.name }}</span>
{% endfor %}
</td>
<td><a href="/del_classes.html?nid={{ row.id }}" rel="external nofollow" rel="external nofollow" >刪除</a>
|<a href="/edit_classes.html?nid={{ row.id }}" rel="external nofollow" rel="external nofollow" >編輯</a>
|<a href="/set_teachers.html?nid={{ row.id }}" rel="external nofollow" >分配老師</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div><a href="/add_classes.html" rel="external nofollow" rel="external nofollow" >添加</a> </div>
</body>
</html>
8.初識Ajax
Ajax是異步傳輸方式,偷偷的向后臺發(fā)請求,不引起頁面刷新,下面通過一個小例子來認識Ajax這種數(shù)據(jù)傳輸方式。
首先下載jQuery導入項目下的static目錄下
1.在app01/Views目錄下新建ajax.py
from django.shortcuts import render,redirect,HttpResponse
def ajax1(request):
return render(request,'ajax1.html')
def ajax2(request):
u=request.GET.get('username')
p=request.GET.get('password')
return HttpResponse('我愿意')
2.在urls.py中配置相關路由
from django.contrib import admin
from django.urls import path
from app01.views import classes,students,teachers,ajax
urlpatterns = [
path('admin/', admin.site.urls),
path('classes.html', classes.get_classes),
path('add_classes.html', classes.add_classes),
path('del_classes.html', classes.del_classes),
path('edit_classes.html', classes.edit_classes),
path('students.html', students.get_students),
path('add_students.html', students.add_students),
path('del_students.html', students.del_students),
path('edit_students.html', students.edit_students),
path('set_teachers.html', classes.set_teachers),
path('ajax1.html', ajax.ajax1),
path('ajax2.html', ajax.ajax2),
]
3.在template目錄下新建ajax1.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.btn{
display: inline-block;
padding: 5px 15px;
background-color: coral;
color: white;
cursor: pointer;
}
</style>
</head>
<body>
<div>
<input placeholder="用戶名" type="text">
<input placeholder="密碼" type="password">
<div class="btn">提交</div>
</div>
<script src="/static/jquery-3.3.1.js"></script>
<script>
function submitForm() {
var u=$('#username').val();
var p=$('#password').val();
$.ajax({
url:'ajax2.html',
type:'GET',
data:{username:u,password:p},
success:function (arg) {
//回調函數(shù) arg是服務器返回的字符串
console.log(arg)
}
})
}
</script>
</body>
</html>
9.學員管理系統(tǒng)之Ajax刪除學員: 1.在ajax.py中增加ajax4函數(shù)
from app01 import models
def ajax4(request):
nid=request.GET.get('nid')
msg='成功'
try:
models.Students.objects.get(id=nid).delete()
except Exception as e:
msg=str(e)
return HttpResponse(msg)
2.在urls.py中配置相關路由
from django.contrib import admin
from django.urls import path
from app01.views import classes,students,teachers,ajax
urlpatterns = [
path('admin/', admin.site.urls),
path('classes.html', classes.get_classes),
path('add_classes.html', classes.add_classes),
path('del_classes.html', classes.del_classes),
path('edit_classes.html', classes.edit_classes),
path('students.html', students.get_students),
path('add_students.html', students.add_students),
path('del_students.html', students.del_students),
path('edit_students.html', students.edit_students),
path('set_teachers.html', classes.set_teachers),
path('ajax1.html', ajax.ajax1),
path('ajax2.html', ajax.ajax2),
path('ajax4.html', ajax.ajax4),
]
3.對get_students.html進行添加修改:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<table>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年齡</th>
<th>性別</th>
<th>班級</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for row in stu_list %}
<tr nid="{{ row.id }}">
<td>{{ row.id }}</td>
<td>{{ row.username }}</td>
<td>{{ row.age }}</td>
<td>{{ row.gender }}</td>
<td>{{ row.cs.title }}</td>
<td><a href="/del_students.html?nid={{ row.id }}" rel="external nofollow" rel="external nofollow" >刪除</a>
|<a href="#" rel="external nofollow" >Ajax刪除</a>
|<a href="/edit_students.html?nid={{ row.id }}" rel="external nofollow" rel="external nofollow" >編輯</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div><a href="/add_students.html" rel="external nofollow" rel="external nofollow" >添加</a> </div>
</body>
<script src="/static/jquery-3.3.1.js"></script>
<script>
function removeStudent(ths) {
var nid=$(ths).parent().parent().attr('nid');
$.ajax({
url:'/ajax4.html',
type:'GET',
data:{nid:nid},
success:function (arg) {
if (arg=='成功'){
window.location.reload();
}else{
alert(arg);
}
}
})
}
</script>
</html>
相關文章
分布式訓練training-operator和pytorch-distributed?RANK變量不統(tǒng)一解決
這篇文章主要介紹了分布式訓練training-operator和pytorch-distributed?RANK變量不統(tǒng)一問題的解決方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04
Python實現(xiàn)獲取系統(tǒng)臨時目錄及臨時文件的方法示例
這篇文章主要介紹了Python實現(xiàn)獲取系統(tǒng)臨時目錄及臨時文件的方法,結合實例形式分析了Python文件與目錄操作相關函數(shù)與使用技巧,需要的朋友可以參考下2019-06-06
解決python多線程報錯:AttributeError: Can''t pickle local object問題
這篇文章主要介紹了解決python多線程報錯:AttributeError: Can't pickle local object問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
對python中raw_input()和input()的用法詳解
下面小編就為大家分享一篇對python中raw_input()和input()的用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
利用Pyhton中的requests包進行網頁訪問測試的方法
今天小編就為大家分享一篇利用Pyhton中的requests包進行網頁訪問測試的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Python機器學習之K-Means聚類實現(xiàn)詳解
這篇文章主要為大家詳細介紹了Python機器學習之K-Means聚類的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02

