Skip to content

Commit

Permalink
更新了Django第三天代码
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfrued committed May 23, 2018
1 parent 9eaf653 commit 96719b5
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
venv
.idea
*.pyc
__pycache__

41 changes: 0 additions & 41 deletions Day32/oa/hrs/migrations/0001_initial.py

This file was deleted.

10 changes: 10 additions & 0 deletions Day32/oa/hrs/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
from django.db import models

# ORM - 对象关系映射
# 对象模型 <---> 关系模型
# 实体类 <---> 二维表
# 属性 <---> 列
# 对象 <---> 记录


class Dept(models.Model):
no = models.IntegerField(primary_key=True, verbose_name='部门编号')
name = models.CharField(max_length=20, verbose_name='部门名称')
location = models.CharField(max_length=10, verbose_name='部门所在地')
excellent = models.BooleanField(default=0, verbose_name='是否优秀')

def __str__(self):
return self.name

class Meta:
db_table = 'tb_dept'
Expand Down
9 changes: 9 additions & 0 deletions Day32/oa/hrs/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path

from hrs import views

urlpatterns = [
path('depts', views.depts, name='depts'),
path('depts/emps', views.emps, name='empsindept'),
path('deldepts', views.del_dept, name='ddel')
]
23 changes: 19 additions & 4 deletions Day32/oa/hrs/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.shortcuts import render
from django.shortcuts import render, redirect
from django.urls import reverse

from hrs.models import Dept, Emp

Expand All @@ -10,12 +11,26 @@ def index(request):
return render(request, 'index.html', context=ctx)


def del_dept(request):
# 重定向 - 重新请求一个指定的页面
return redirect(reverse('depts'))


def emps(request):
dno = int(request.GET['dno'])
no = request.GET['no']
# dept = Dept.objects.get(no=no)
# ForeignKey(Dept, on_delete=models.PROTECT, related_name='emps')
# dept.emps.all()
# emps_list = dept.emp_set.all()
# all() / filter() ==> QuerySet
# QuerySet使用了惰性查询 - 如果不是非得取到数据那么不会发出SQL语句
# 这样做是为了节省服务器内存的开销 - 延迟加载 - 节省空间势必浪费时间
emps_list = list(Emp.objects.filter(dept__no=no).select_related('dept'))
ctx = {'emp_list': emps_list, 'dept_name': emps_list[0].dept.name} \
if len(emps_list) > 0 else {}
return render(request, 'emp.html', context=ctx)


def depts(request):
# DRY - Don't Repeat Yourself
# ORM - Object Relation Mapping
ctx = {'dept_list': Dept.objects.all()}
return render(request, 'dept.html', context=ctx)
18 changes: 18 additions & 0 deletions Day32/oa/oa/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,21 @@
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

STATIC_URL = '/static/'

# 配置将日志输出到控制台 日志级别为DEBUG(最详细的日志)
# DEBUG < INFO < WARNING < ERROR < CRITICAL
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': os.getenv('DJANGO_LOG_LEVEL', 'DEBUG'),
},
},
}
4 changes: 2 additions & 2 deletions Day32/oa/oa/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include

from hrs import views

urlpatterns = [
path('', views.index),
path('admin/', admin.site.urls),
path('hrs/depts', views.depts)
path('hrs/', include('hrs.urls')),
]
13 changes: 11 additions & 2 deletions Day32/oa/templates/dept.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ <h3>部门信息</h3>
<th>部门编号</th>
<th>部门名称</th>
<th>部门所在地</th>
<th>是否优秀</th>
<th>操作</th>
</tr>
</thead>
Expand All @@ -30,11 +31,19 @@ <h3>部门信息</h3>
<tr>
<td>{{ dept.no }}</td>
<td>
<a href="/hrs/emps?dno={{ dept.no }}">{{ dept.name }}</a>
<!-- 写代码时要尽量避免使用硬编码(hard code) -->
<a href="{% url 'empsindept' %}?no={{ dept.no }}">{{ dept.name }}</a>
</td>
<td>{{ dept.location }}</td>
<td>
<a href="/hrs/deldept?dno={{ dept.no }}" class="btn btn-xs btn-warning">删除</a>
{% if dept.excellent %}
<span style="color: green;"></span>
{% else %}
<span style="color: red;">×</span>
{% endif %}
</td>
<td>
<a href="{% url 'ddel' %}?dno={{ dept.no }}" class="btn btn-xs btn-warning">删除</a>
</td>
</tr>
{% endfor %}
Expand Down
60 changes: 60 additions & 0 deletions Day32/oa/templates/emp.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>员工</title>
<link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<h3>{{ dept_name }}员工信息</h3>
<hr>
</div>
</div>
<div class="row clearfix">
<div class="col-md-8 column">
{% if emp_list %}
<table id="dept" class="table table-striped table-hover">
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
<th>职位</th>
<th>月薪</th>
<th>部门名称</th>
</tr>
</thead>
<tbody>
{% for emp in emp_list %}
<tr>
<td>{{ emp.no }}</td>
<td>{{ emp.name }}</td>
<td>{{ emp.job }}</td>
<td>{{ emp.sal }}</td>
<td>{{ dept_name }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<h2>此部门暂时没有员工!</h2>
{% endif %}
</div>
<div class="col-md-4 column">
</div>
</div>
<a href="{% url 'depts' %}">返回部门列表</a>
</div>
<script src="{% static 'js/jquery.min.js' %}"></script>
<script src="{% static 'js/bootstrap.min.js' %}"></script>
<script>
$(function() {
$('#dept tbody tr:even').addClass('info');
$('#dept tbody tr:odd').addClass('warning');
});
</script>
</body>
</html>

0 comments on commit 96719b5

Please sign in to comment.