作业:
a)新建一个干净项目,项目名为day7+名字首拼
b)编写部门Department模型类,由部门名称dep_name、部门描述desc组成,并自己添加3条数据
c)编写员工Employee模型类,姓名emp_name、职位job、工资salary、部门department,并自己添加测试数据
d)实现以下功能:
主页面功能:
1.展示所有的部门信息
2.可以新增员工信息
3.点击部门名可以进入到指定部门下,展示对应部门的员工信息
员工信息页面功能:
django-admin startproject day7df
在创建一个子应用
python manage.py startapp myapp
create database day7df charset=utf8;
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp', # 注册子应用
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# 需要在目录下创建一个template文件夹
'DIRS': [os.path.join(BASE_DIR, 'template')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
# 配置mysql信息
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1',
'PORT': 3306,
'USER': 'root',
'PASSWORD': '自己的密码',
'NAME': 'day7df'
}
}
# 编译成中文
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
import pymysql
pymysql.install_as_MySQLdb()
from django.db import models
# Create your models here.
class Department(models.Model):
dep_name = models.CharField(max_length=20, verbose_name='部门名称')
desc = models.CharField(max_length=20, verbose_name='部门描述')
class Meta:
db_table = 'department'
verbose_name = '部门'
verbose_name_plural = verbose_name
def __str__(self):
return self.dep_name
class Employee(models.Model):
emp_name = models.CharField(max_length=20, verbose_name='员工')
job = models.CharField(max_length=20, verbose_name='职位')
salary = models.IntegerField(verbose_name='工资')
# 外键链接
department = models.ForeignKey(to=Department, on_delete=models.CASCADE, verbose_name='部门')
verbose_name='身高')
class Meta:
db_table = 'employee'
verbose_name = '职员'
verbose_name_plural = verbose_name
def __str__(self):
return self.emp_name
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
from django.shortcuts import render,redirect
from django.http import HttpResponse
from django.views import View
from myapp.models import Department,Employee
# Create your views here.
# 展示部门信息
class DepartmentView(View):
def get(self, request):
dep_data = Department.objects.all()
return render(request, 'list.html', {'daps': dep_data})
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('dep/', views.DepartmentView.as_view()),
]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>部门信息</h3>
<table border="1">
<tr>
<th>编号</th>
<th>部门名称</th>
<th>部门描述</th>
</tr>
{% for dap in daps %}
<tr>
<td>{{ dap.id }}</td>
<td>{{ dap.dep_name }}</td>
<td>{{ dap.desc }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
# 展示员工信息
class EmployeeView(View):
def get(self, request, id):
emp_data = Employee.objects.filter(department_id=id)
return render(request, 'emplist.html', {'emps': emp_data})
path('emp//' , views.EmployeeView.as_view()),
<td>
<a href="/emp/{{ dap.id }}/">{{ dap.dep_name }}</a>
</td>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>员工信息</h3>
<table border="1">
<tr>
<th>编号</th>
<th>姓名</th>
<th>职位</th>
<th>工资</th>
<th>部门</th>
</tr>
{% for emp in emps %}
<tr>
<td>{{ emp.id }}</td>
<td>{{ emp.emp_name }}</td>
<td>{{ emp.job }}</td>
<td>{{ emp.salary }}</td>
<td>{{ emp.department }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
<h3>添加数据</h3>
<form method="post">
{% csrf_token %}
姓名:<input type="text" name="emp_name"><br>
职位:<input type="text" name="job"><br>
工资:<input type="text" name="salary"><br>
部门:<input type="text" name="department"><br>
<input type="submit" value="添加">
</form>
# 增加数据
def post(self, request):
emp_name = request.POST.get('emp_name')
job = request.POST.get('job')
salary = request.POST.get('salary')
department = request.POST.get('department')
try:
Employee.objects.create(
emp_name=emp_name,
job=job,
salary=salary,
department_id=department
)
except Employee as a:
print(a)
return HttpResponse('添加失败')
return redirect('/dep/')
# 修改数据
class UpdEmployee(View):
def get(self, request, id):
upd_data = Employee.objects.get(id=id)
return render(request, 'update.html', {'upd': upd_data})
def post(self, request, id):
emp_name = request.POST.get('emp_name')
job = request.POST.get('job')
salary = request.POST.get('salary')
department = request.POST.get('department')
try:
Employee.objects.filter(id=id).update(
emp_name=emp_name,
job=job,
salary=salary,
department_id=department
)
except Employee as a:
print(a)
return HttpResponse('修改失败')
return redirect('/dep/')
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>修改数据</h3>
<form method="post">
{% csrf_token %}
姓名:<input type="text" value="{{ upd.emp_name }}" name="emp_name"><br>
职位:<input type="text" value="{{ upd.job }}" name="job"><br>
工资:<input type="text" value="{{ upd.salary }}" name="salary"><br>
部门:<input type="text" value="{{ upd.department_id }}" name="department"><br>
<input type="submit" value="修改">
</form>
</body>
</html>
path('upd//' , views.UpdEmployee.as_view()),
<td>
<a href="/upd/{{ emp.id }}/">修改</a>
</td>
# 删除信息
class DelEmployee(View):
def get(self, request, id):
emp_del = Employee.objects.get(id=id)
sel_emp = emp_del.department_id
emp_del.delete()
return redirect(f'/emp/{sel_emp}/')
path('del//' , views.DelEmployee.as_view()),
<td>
<a href="/upd/{{ emp.id }}/">修改</a>
<a href="/del/{{ emp.id }}/">删除</a>
</td>