Skip to content

Commit

Permalink
添加了第32天的代码
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfrued committed May 22, 2018
1 parent a0e51bf commit 09a4ec0
Show file tree
Hide file tree
Showing 20 changed files with 398 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
venv
.idea
*.pyc
Empty file added Day32/oa/hrs/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions Day32/oa/hrs/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.contrib import admin

from hrs.models import Dept, Emp


class DeptAdmin(admin.ModelAdmin):

list_display = ('no', 'name', 'location')
ordering = ('no', )


class EmpAdmin(admin.ModelAdmin):

list_display = ('no', 'name', 'job', 'sal', 'dept')
search_fields = ('name', 'job')
ordering = ('dept', )


admin.site.register(Dept, DeptAdmin)
admin.site.register(Emp, EmpAdmin)
5 changes: 5 additions & 0 deletions Day32/oa/hrs/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class HrsConfig(AppConfig):
name = 'hrs'
41 changes: 41 additions & 0 deletions Day32/oa/hrs/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated by Django 2.0.5 on 2018-05-22 03:07

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Dept',
fields=[
('no', models.IntegerField(primary_key=True, serialize=False)),
('name', models.CharField(max_length=20)),
('location', models.CharField(max_length=10)),
],
options={
'db_table': 'tb_dept',
},
),
migrations.CreateModel(
name='Emp',
fields=[
('no', models.IntegerField(primary_key=True, serialize=False)),
('name', models.CharField(max_length=20)),
('job', models.CharField(max_length=10)),
('mgr', models.IntegerField(null=True)),
('sal', models.DecimalField(decimal_places=2, max_digits=7)),
('comm', models.DecimalField(decimal_places=2, max_digits=7, null=True)),
('dept', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='hrs.Dept')),
],
options={
'db_table': 'tb_emp',
},
),
]
Empty file.
33 changes: 33 additions & 0 deletions Day32/oa/hrs/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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'


class Emp(models.Model):
no = models.IntegerField(primary_key=True)
name = models.CharField(max_length=20)
job = models.CharField(max_length=10)
mgr = models.IntegerField(null=True, blank=True)
sal = models.DecimalField(max_digits=7, decimal_places=2)
comm = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True)
dept = models.ForeignKey(Dept, on_delete=models.PROTECT)

class Meta:
db_table = 'tb_emp'
3 changes: 3 additions & 0 deletions Day32/oa/hrs/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
21 changes: 21 additions & 0 deletions Day32/oa/hrs/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.shortcuts import render

from hrs.models import Dept, Emp


def index(request):
ctx = {
'greeting': '你好,世界!'
}
return render(request, 'index.html', context=ctx)


def emps(request):
dno = int(request.GET['dno'])


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)
15 changes: 15 additions & 0 deletions Day32/oa/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "oa.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
3 changes: 3 additions & 0 deletions Day32/oa/oa/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import pymysql

pymysql.install_as_MySQLdb()
128 changes: 128 additions & 0 deletions Day32/oa/oa/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
"""
Django settings for oa project.
Generated by 'django-admin startproject' using Django 2.0.5.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'c^dt134g38w^r4+0f$dpoe)1d5)q1kn+2%g--#!*+xvvn&93=_'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'hrs',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'oa.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'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',
],
},
},
]

WSGI_APPLICATION = 'oa.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'oa',
'HOST': 'localhost',
'PORT': 3306,
'USER': 'root',
'PASSWORD': '123456'
}
}


# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/Chongqing'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

STATIC_URL = '/static/'
25 changes: 25 additions & 0 deletions Day32/oa/oa/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""oa URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

from hrs import views

urlpatterns = [
path('', views.index),
path('admin/', admin.site.urls),
path('hrs/depts', views.depts)
]
16 changes: 16 additions & 0 deletions Day32/oa/oa/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for oa project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "oa.settings")

application = get_wsgi_application()
6 changes: 6 additions & 0 deletions Day32/oa/static/css/bootstrap.min.css

Large diffs are not rendered by default.

Binary file added Day32/oa/static/images/mm.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions Day32/oa/static/js/bootstrap.min.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Day32/oa/static/js/jquery.min.js

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions Day32/oa/templates/dept.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!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>部门信息</h3>
<hr>
</div>
</div>
<div class="row clearfix">
<div class="col-md-8 column">
<table id="dept" class="table table-striped table-hover">
<thead>
<tr>
<th>部门编号</th>
<th>部门名称</th>
<th>部门所在地</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for dept in dept_list %}
<tr>
<td>{{ dept.no }}</td>
<td>
<a href="/hrs/emps?dno={{ 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>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="col-md-4 column">
</div>
</div>
</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>
13 changes: 13 additions & 0 deletions Day32/oa/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
{% load staticfiles %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>{{ greeting }}</h1>
<hr>
<img src="{% static 'images/mm.jpg' %}" alt="">
</body>
</html>

0 comments on commit 09a4ec0

Please sign in to comment.