Skip to content

Commit 09a4ec0

Browse files
committed
添加了第32天的代码
1 parent a0e51bf commit 09a4ec0

20 files changed

+398
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
venv
2+
.idea
3+
*.pyc

Day32/oa/hrs/__init__.py

Whitespace-only changes.

Day32/oa/hrs/admin.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from django.contrib import admin
2+
3+
from hrs.models import Dept, Emp
4+
5+
6+
class DeptAdmin(admin.ModelAdmin):
7+
8+
list_display = ('no', 'name', 'location')
9+
ordering = ('no', )
10+
11+
12+
class EmpAdmin(admin.ModelAdmin):
13+
14+
list_display = ('no', 'name', 'job', 'sal', 'dept')
15+
search_fields = ('name', 'job')
16+
ordering = ('dept', )
17+
18+
19+
admin.site.register(Dept, DeptAdmin)
20+
admin.site.register(Emp, EmpAdmin)

Day32/oa/hrs/apps.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class HrsConfig(AppConfig):
5+
name = 'hrs'
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Generated by Django 2.0.5 on 2018-05-22 03:07
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
initial = True
10+
11+
dependencies = [
12+
]
13+
14+
operations = [
15+
migrations.CreateModel(
16+
name='Dept',
17+
fields=[
18+
('no', models.IntegerField(primary_key=True, serialize=False)),
19+
('name', models.CharField(max_length=20)),
20+
('location', models.CharField(max_length=10)),
21+
],
22+
options={
23+
'db_table': 'tb_dept',
24+
},
25+
),
26+
migrations.CreateModel(
27+
name='Emp',
28+
fields=[
29+
('no', models.IntegerField(primary_key=True, serialize=False)),
30+
('name', models.CharField(max_length=20)),
31+
('job', models.CharField(max_length=10)),
32+
('mgr', models.IntegerField(null=True)),
33+
('sal', models.DecimalField(decimal_places=2, max_digits=7)),
34+
('comm', models.DecimalField(decimal_places=2, max_digits=7, null=True)),
35+
('dept', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='hrs.Dept')),
36+
],
37+
options={
38+
'db_table': 'tb_emp',
39+
},
40+
),
41+
]

Day32/oa/hrs/migrations/__init__.py

Whitespace-only changes.

Day32/oa/hrs/models.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from django.db import models
2+
3+
# ORM - 对象关系映射
4+
# 对象模型 <---> 关系模型
5+
# 实体类 <---> 二维表
6+
# 属性 <---> 列
7+
# 对象 <---> 记录
8+
9+
10+
class Dept(models.Model):
11+
no = models.IntegerField(primary_key=True, verbose_name='部门编号')
12+
name = models.CharField(max_length=20, verbose_name='部门名称')
13+
location = models.CharField(max_length=10, verbose_name='部门所在地')
14+
# excellent = models.BooleanField(default=0, verbose_name='是否优秀')
15+
16+
def __str__(self):
17+
return self.name
18+
19+
class Meta:
20+
db_table = 'tb_dept'
21+
22+
23+
class Emp(models.Model):
24+
no = models.IntegerField(primary_key=True)
25+
name = models.CharField(max_length=20)
26+
job = models.CharField(max_length=10)
27+
mgr = models.IntegerField(null=True, blank=True)
28+
sal = models.DecimalField(max_digits=7, decimal_places=2)
29+
comm = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True)
30+
dept = models.ForeignKey(Dept, on_delete=models.PROTECT)
31+
32+
class Meta:
33+
db_table = 'tb_emp'

Day32/oa/hrs/tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

Day32/oa/hrs/views.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from django.shortcuts import render
2+
3+
from hrs.models import Dept, Emp
4+
5+
6+
def index(request):
7+
ctx = {
8+
'greeting': '你好,世界!'
9+
}
10+
return render(request, 'index.html', context=ctx)
11+
12+
13+
def emps(request):
14+
dno = int(request.GET['dno'])
15+
16+
17+
def depts(request):
18+
# DRY - Don't Repeat Yourself
19+
# ORM - Object Relation Mapping
20+
ctx = {'dept_list': Dept.objects.all()}
21+
return render(request, 'dept.html', context=ctx)

Day32/oa/manage.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
5+
if __name__ == "__main__":
6+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "oa.settings")
7+
try:
8+
from django.core.management import execute_from_command_line
9+
except ImportError as exc:
10+
raise ImportError(
11+
"Couldn't import Django. Are you sure it's installed and "
12+
"available on your PYTHONPATH environment variable? Did you "
13+
"forget to activate a virtual environment?"
14+
) from exc
15+
execute_from_command_line(sys.argv)

Day32/oa/oa/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import pymysql
2+
3+
pymysql.install_as_MySQLdb()

Day32/oa/oa/settings.py

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
"""
2+
Django settings for oa project.
3+
4+
Generated by 'django-admin startproject' using Django 2.0.5.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/2.0/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/2.0/ref/settings/
11+
"""
12+
13+
import os
14+
15+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17+
18+
19+
# Quick-start development settings - unsuitable for production
20+
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
21+
22+
# SECURITY WARNING: keep the secret key used in production secret!
23+
SECRET_KEY = 'c^dt134g38w^r4+0f$dpoe)1d5)q1kn+2%g--#!*+xvvn&93=_'
24+
25+
# SECURITY WARNING: don't run with debug turned on in production!
26+
DEBUG = True
27+
28+
ALLOWED_HOSTS = []
29+
30+
31+
# Application definition
32+
33+
INSTALLED_APPS = [
34+
'django.contrib.admin',
35+
'django.contrib.auth',
36+
'django.contrib.contenttypes',
37+
'django.contrib.sessions',
38+
'django.contrib.messages',
39+
'django.contrib.staticfiles',
40+
'hrs',
41+
]
42+
43+
MIDDLEWARE = [
44+
'django.middleware.security.SecurityMiddleware',
45+
'django.contrib.sessions.middleware.SessionMiddleware',
46+
'django.middleware.common.CommonMiddleware',
47+
'django.middleware.csrf.CsrfViewMiddleware',
48+
'django.contrib.auth.middleware.AuthenticationMiddleware',
49+
'django.contrib.messages.middleware.MessageMiddleware',
50+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
51+
]
52+
53+
ROOT_URLCONF = 'oa.urls'
54+
55+
TEMPLATES = [
56+
{
57+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
58+
'DIRS': [os.path.join(BASE_DIR, 'templates')]
59+
,
60+
'APP_DIRS': True,
61+
'OPTIONS': {
62+
'context_processors': [
63+
'django.template.context_processors.debug',
64+
'django.template.context_processors.request',
65+
'django.contrib.auth.context_processors.auth',
66+
'django.contrib.messages.context_processors.messages',
67+
],
68+
},
69+
},
70+
]
71+
72+
WSGI_APPLICATION = 'oa.wsgi.application'
73+
74+
75+
# Database
76+
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
77+
78+
DATABASES = {
79+
'default': {
80+
'ENGINE': 'django.db.backends.mysql',
81+
'NAME': 'oa',
82+
'HOST': 'localhost',
83+
'PORT': 3306,
84+
'USER': 'root',
85+
'PASSWORD': '123456'
86+
}
87+
}
88+
89+
90+
# Password validation
91+
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
92+
93+
AUTH_PASSWORD_VALIDATORS = [
94+
{
95+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
96+
},
97+
{
98+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
99+
},
100+
{
101+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
102+
},
103+
{
104+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
105+
},
106+
]
107+
108+
109+
# Internationalization
110+
# https://docs.djangoproject.com/en/2.0/topics/i18n/
111+
112+
LANGUAGE_CODE = 'zh-hans'
113+
114+
TIME_ZONE = 'Asia/Chongqing'
115+
116+
USE_I18N = True
117+
118+
USE_L10N = True
119+
120+
USE_TZ = True
121+
122+
123+
# Static files (CSS, JavaScript, Images)
124+
# https://docs.djangoproject.com/en/2.0/howto/static-files/
125+
126+
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
127+
128+
STATIC_URL = '/static/'

Day32/oa/oa/urls.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""oa URL Configuration
2+
3+
The `urlpatterns` list routes URLs to views. For more information please see:
4+
https://docs.djangoproject.com/en/2.0/topics/http/urls/
5+
Examples:
6+
Function views
7+
1. Add an import: from my_app import views
8+
2. Add a URL to urlpatterns: path('', views.home, name='home')
9+
Class-based views
10+
1. Add an import: from other_app.views import Home
11+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12+
Including another URLconf
13+
1. Import the include() function: from django.urls import include, path
14+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15+
"""
16+
from django.contrib import admin
17+
from django.urls import path
18+
19+
from hrs import views
20+
21+
urlpatterns = [
22+
path('', views.index),
23+
path('admin/', admin.site.urls),
24+
path('hrs/depts', views.depts)
25+
]

Day32/oa/oa/wsgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for oa project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "oa.settings")
15+
16+
application = get_wsgi_application()

Day32/oa/static/css/bootstrap.min.css

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Day32/oa/static/images/mm.jpg

614 KB
Loading

Day32/oa/static/js/bootstrap.min.js

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Day32/oa/static/js/jquery.min.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Day32/oa/templates/dept.html

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!DOCTYPE html>
2+
{% load static %}
3+
<html lang="en">
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>部门</title>
7+
<link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">
8+
</head>
9+
<body>
10+
<div class="container">
11+
<div class="row clearfix">
12+
<div class="col-md-12 column">
13+
<h3>部门信息</h3>
14+
<hr>
15+
</div>
16+
</div>
17+
<div class="row clearfix">
18+
<div class="col-md-8 column">
19+
<table id="dept" class="table table-striped table-hover">
20+
<thead>
21+
<tr>
22+
<th>部门编号</th>
23+
<th>部门名称</th>
24+
<th>部门所在地</th>
25+
<th>操作</th>
26+
</tr>
27+
</thead>
28+
<tbody>
29+
{% for dept in dept_list %}
30+
<tr>
31+
<td>{{ dept.no }}</td>
32+
<td>
33+
<a href="/hrs/emps?dno={{ dept.no }}">{{ dept.name }}</a>
34+
</td>
35+
<td>{{ dept.location }}</td>
36+
<td>
37+
<a href="/hrs/deldept?dno={{ dept.no }}" class="btn btn-xs btn-warning">删除</a>
38+
</td>
39+
</tr>
40+
{% endfor %}
41+
</tbody>
42+
</table>
43+
</div>
44+
<div class="col-md-4 column">
45+
</div>
46+
</div>
47+
</div>
48+
<script src="{% static 'js/jquery.min.js' %}"></script>
49+
<script src="{% static 'js/bootstrap.min.js' %}"></script>
50+
<script>
51+
$(function() {
52+
$('#dept tbody tr:even').addClass('info');
53+
$('#dept tbody tr:odd').addClass('warning');
54+
});
55+
</script>
56+
</body>
57+
</html>

Day32/oa/templates/index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
{% load staticfiles %}
3+
<html lang="en">
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>首页</title>
7+
</head>
8+
<body>
9+
<h1>{{ greeting }}</h1>
10+
<hr>
11+
<img src="{% static 'images/mm.jpg' %}" alt="">
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)