from django.db import models
"""
实体类
"""
class Department(models.Model):
dname = models.CharField(max_length=100,unique=True,null=False,verbose_name="部门名称")
isDelete = models.BooleanField(default=False)
def __str__(self):
return "Department(dname=%s)" % self.dname
class Meta:
db_table = 'department_info'
verbose_name = "部门表"
verbose_name_plural = "部门表"
class Staff(models.Model):
sname = models.CharField(max_length=100,unique=True,null=False, verbose_name="员工名称")
sdepartment = models.ForeignKey(Department,null=True)
isDelete = models.BooleanField(default=False)
bpubdate = models.DateField(null=False)
def __str__(self):
return "Staff(sname=%s)" % self.sname
class Meta:
db_table = 'staff_info'
verbose_name = "员工表"
verbose_name_plural = "员工表"
三,bumen下的url.py
from django.conf.urls import url ,include
from django.contrib import admin
from bumen import views
"""
这里一定加$
"""
#
urlpatterns = [
url(r'^staff$', views.staff_select_all, name="staff_select_all"),
url(r'^staff_edit$', views.staff_edit, name='staff_edit'),
url(r'^staff_edit_handler$', views.staff_edit_handler,name='staff_edit_handler'),
url(r'^staff_delete$', views.staff_delete, name='staff_delete'),
url(r'^staff_insert$', views.staff_insert, name='staff_insert'),
url(r'^staff_insert_handler$', views.staff_insert_handler, name='staff_insert_handler'),
url(r'^getsname$', views.getsname, name='getsname'),
]
四,bumen下的view.py
from django.shortcuts import render
from django.core.urlresolvers import reverse
from django.shortcuts import render,HttpResponse,redirect
from bumen.models import *
import time, datetime
import json
def staff_select_all(request):
staff_list = Staff.objects.all()
for i in staff_list:
i.bpubdate = i.bpubdate.strftime("%Y/%m/%d")
return render(request, 'bumen/staff_list.html', {'staff_list': staff_list})
def staff_edit(request):
#准备数据1
sid=request.GET.get('sid')
staff=Staff.objects.get(id=sid)
# 准备数据2
department_list = Department.objects.all()
return render(request,'bumen/staff_edit.html',{'staff':staff,'department_list':department_list})
def staff_edit_handler(request):
#接受参数
sid=request.POST.get('sid')
sname = request.POST.get('sname')
sdepartment_id = request.POST.get('sdepartment_id')
isDelete = request.POST.get('isDelete',False)
# 查询
staff = Staff.objects.get(id=sid)
'''更新数据库'''
#修改
staff.sname=sname
staff.sdepartment_id = sdepartment_id
staff.isDelete = isDelete
staff.save()
return redirect(reverse('bumen:staff_select_all'))
def staff_delete(request):
print('111')
# 接受参数
sid = request.GET.get('sid')
# 查询
staff = Staff.objects.get(id=sid)
# 删除
staff.delete()
return redirect(reverse('bumen:staff_select_all'))
def staff_insert(request):
# 准备数据:所有的Department
department_list = Department.objects.all()
return render(request,'bumen/staff_insert.html',{'department_list':department_list})
def staff_insert_handler(request):
#接受参数
sname = request.POST.get('sname')
sdepartment_id = request.POST.get('sdepartment_id')
bpubdate = request.POST.get('bpubdate')
#新增
staff=Staff()
staff.sname=sname
staff.sdepartment_id = sdepartment_id
staff.bpubdate=bpubdate
staff.save()
return redirect(reverse('bumen:staff_select_all'))
def getsname(request):
sname = request.GET.get("sname")
if sname == "":
ret = {
"ret": []
}
return HttpResponse(json.dumps(ret, ensure_ascii=False), content_type="application/json;charset=utf-8")
sname_list = Staff.objects.filter(sname__icontains=sname).values("sname") #icontains:忽略大小写状态下的模糊查询,contains: 精确大小写状态下的模糊查询
info = []
for i in sname_list:
d = {}
d["sname"] = i["sname"]
info.append(d)
ret = {
"ret": info
}
return HttpResponse(json.dumps(ret, ensure_ascii=False), content_type="application/json;charset=utf-8")
五,templates/bumen下的3个html文件
1.查询删除staff_list.html
staff_list.html
{% for i in staff_list %}
{{ forloop.counter}}
{{i.sname}}
{{i.sdepartment.dname}}
{{i.bpubdate}}
编辑
删除
{% endfor %}
2.增加staff_insert.html
staff_insert.html
3.修改staff_edit.html
staff_list.html