STATICFILES_DIRS=(os.path.join(BASE_DIR, 'static'),)
TEMPLATES = [
{
'BACKEND': '...',
'DIRS': [os.path.join(BASE_DIR, 'templates'),],
'APP_DIRS': ...,
'OPTIONS': {
'context_processors': [
...
],
},
},
]
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),
path('ajax1.html', ajax.ajax1),
path('ajax2.html', ajax.ajax2),
path('ajax4.html', ajax.ajax4),
]
from django.db import models
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)
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')
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')
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<table border="1">
<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 }}">删除</a>
|<a href="/edit_classes.html?nid={{ row.id }}">编辑</a>
|<a href="/set_teachers.html?nid={{ row.id }}">分配老师</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div><a href="/add_classes.html">添加</a> </div>
</body>
</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>
<!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>
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')
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<table border="1">
<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 }}">删除</a>
|<a onclick="removeStudent(this);" href="#">Ajax删除</a>
|<a href="/edit_students.html?nid={{ row.id }}">编辑</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div><a href="/add_students.html">添加</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>
<!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>
<!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>
<!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>
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('我愿意')
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)
<!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" id="username">
<input placeholder="密码" type="password" id="password">
<div class="btn" onclick="submitForm();">提交</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) {
console.log(arg)
}
})
}
</script>
</body>
</html>
总结2种方式:
1.刷新页面:
增加/编辑记录:推荐刷新页面
优点:独立的页面;代码量相对较少
适用于:字段多的情况
2.对话框+Ajax:
增加/编辑记录:除非需要考虑性能,才使用Ajax
优点:对Server压力小;局部刷新;异步请求,不妨碍其他操作
适用于:字段少的情况
注意:考虑是否在当前页还是在首/尾页添加记录;自定义属性的添加
删除记录:永远使用Ajax