Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
泡泡吐肥皂o
Python-100-Days
提交
4a458a59
P
Python-100-Days
项目概览
泡泡吐肥皂o
/
Python-100-Days
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Python-100-Days
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4a458a59
编写于
7月 04, 2018
作者:
骆昊的技术专栏
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
'更新了Django示例代码'
上级
b265f0c8
变更
16
展开全部
隐藏空白更改
内联
并排
Showing
16 changed file
with
239 addition
and
99 deletion
+239
-99
Day41-55/code/hellodjango/db.sqlite3
Day41-55/code/hellodjango/db.sqlite3
+0
-0
Day41-55/code/hellodjango/demo/admin.py
Day41-55/code/hellodjango/demo/admin.py
+9
-2
Day41-55/code/hellodjango/demo/migrations/0001_initial.py
Day41-55/code/hellodjango/demo/migrations/0001_initial.py
+21
-6
Day41-55/code/hellodjango/demo/migrations/0002_auto_20180704_1117.py
...de/hellodjango/demo/migrations/0002_auto_20180704_1117.py
+36
-0
Day41-55/code/hellodjango/demo/migrations/0002_teacher_photo.py
...55/code/hellodjango/demo/migrations/0002_teacher_photo.py
+0
-18
Day41-55/code/hellodjango/demo/migrations/0003_auto_20180703_1355.py
...de/hellodjango/demo/migrations/0003_auto_20180703_1355.py
+0
-17
Day41-55/code/hellodjango/demo/migrations/0003_auto_20180704_1118.py
...de/hellodjango/demo/migrations/0003_auto_20180704_1118.py
+23
-0
Day41-55/code/hellodjango/demo/migrations/0004_auto_20180703_1412.py
...de/hellodjango/demo/migrations/0004_auto_20180703_1412.py
+0
-21
Day41-55/code/hellodjango/demo/models.py
Day41-55/code/hellodjango/demo/models.py
+22
-5
Day41-55/code/hellodjango/demo/views.py
Day41-55/code/hellodjango/demo/views.py
+31
-5
Day41-55/code/hellodjango/hellodjango/settings.py
Day41-55/code/hellodjango/hellodjango/settings.py
+1
-1
Day41-55/code/hellodjango/hellodjango/urls.py
Day41-55/code/hellodjango/hellodjango/urls.py
+4
-1
Day41-55/code/hellodjango/static/js/jquery.min.js
Day41-55/code/hellodjango/static/js/jquery.min.js
+2
-0
Day41-55/code/hellodjango/templates/demo/home.html
Day41-55/code/hellodjango/templates/demo/home.html
+0
-23
Day41-55/code/hellodjango/templates/demo/index.html
Day41-55/code/hellodjango/templates/demo/index.html
+18
-0
Day41-55/code/hellodjango/templates/demo/teacher.html
Day41-55/code/hellodjango/templates/demo/teacher.html
+72
-0
未找到文件。
Day41-55/code/hellodjango/db.sqlite3
已删除
100644 → 0
浏览文件 @
b265f0c8
Day41-55/code/hellodjango/demo/admin.py
浏览文件 @
4a458a59
from
django.contrib
import
admin
from
demo.models
import
Teacher
from
demo.models
import
Teacher
,
Subject
class
SubjectAdmin
(
admin
.
ModelAdmin
):
list_display
=
(
'no'
,
'name'
,
'intro'
)
ordering
=
(
'no'
,
)
class
TeacherAdmin
(
admin
.
ModelAdmin
):
list_display
=
(
'no'
,
'name'
,
'
job'
,
'intro'
,
'motto
'
)
list_display
=
(
'no'
,
'name'
,
'
intro'
,
'motto'
,
'subject'
,
'manager
'
)
search_fields
=
(
'name'
,
'intro'
)
ordering
=
(
'no'
,
)
admin
.
site
.
register
(
Subject
,
SubjectAdmin
)
admin
.
site
.
register
(
Teacher
,
TeacherAdmin
)
Day41-55/code/hellodjango/demo/migrations/0001_initial.py
浏览文件 @
4a458a59
# Generated by Django 2.0.6 on 2018-07-0
3 02:2
0
# Generated by Django 2.0.6 on 2018-07-0
4 02:3
0
from
django.db
import
migrations
,
models
import
django.db.models.deletion
class
Migration
(
migrations
.
Migration
):
...
...
@@ -11,17 +12,31 @@ class Migration(migrations.Migration):
]
operations
=
[
migrations
.
CreateModel
(
name
=
'Subject'
,
fields
=
[
(
'no'
,
models
.
AutoField
(
db_column
=
'sno'
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'编号'
)),
(
'name'
,
models
.
CharField
(
db_column
=
'sname'
,
max_length
=
50
,
verbose_name
=
'学科名称'
)),
(
'intro'
,
models
.
CharField
(
db_column
=
'sintro'
,
max_length
=
511
,
verbose_name
=
'学科介绍'
)),
],
options
=
{
'db_table'
:
'tb_subject'
,
},
),
migrations
.
CreateModel
(
name
=
'Teacher'
,
fields
=
[
(
'no'
,
models
.
AutoField
(
db_column
=
'tno'
,
primary_key
=
True
,
serialize
=
False
)),
(
'name'
,
models
.
CharField
(
db_column
=
'tname'
,
max_length
=
20
)),
(
'job'
,
models
.
CharField
(
db_column
=
'tjob'
,
max_length
=
10
)),
(
'intro'
,
models
.
CharField
(
db_column
=
'tintro'
,
max_length
=
1023
)),
(
'motto'
,
models
.
CharField
(
db_column
=
'tmotto'
,
max_length
=
255
)),
(
'no'
,
models
.
AutoField
(
db_column
=
'tno'
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'编号'
)),
(
'name'
,
models
.
CharField
(
db_column
=
'tname'
,
max_length
=
20
,
verbose_name
=
'姓名'
)),
(
'intro'
,
models
.
CharField
(
db_column
=
'tintro'
,
max_length
=
1023
,
verbose_name
=
'简介'
)),
(
'motto'
,
models
.
CharField
(
db_column
=
'tmotto'
,
max_length
=
255
,
verbose_name
=
'教学理念'
)),
(
'photo'
,
models
.
CharField
(
blank
=
True
,
db_column
=
'tphoto'
,
max_length
=
511
,
null
=
True
)),
(
'manager'
,
models
.
BooleanField
(
db_column
=
'tmanager'
,
default
=
False
,
verbose_name
=
'是否主管'
)),
(
'subject'
,
models
.
ForeignKey
(
db_column
=
'sno'
,
on_delete
=
django
.
db
.
models
.
deletion
.
PROTECT
,
to
=
'demo.Subject'
,
verbose_name
=
'所属学科'
)),
],
options
=
{
'db_table'
:
'tb_teacher'
,
'ordering'
:
(
'name'
,),
},
),
]
Day41-55/code/hellodjango/demo/migrations/0002_auto_20180704_1117.py
0 → 100644
浏览文件 @
4a458a59
# Generated by Django 2.0.6 on 2018-07-04 03:17
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'demo'
,
'0001_initial'
),
]
operations
=
[
migrations
.
AlterModelOptions
(
name
=
'subject'
,
options
=
{
'verbose_name'
:
'学科'
,
'verbose_name_plural'
:
'学科'
},
),
migrations
.
AlterModelOptions
(
name
=
'teacher'
,
options
=
{
'ordering'
:
(
'name'
,),
'verbose_name'
:
'讲师'
,
'verbose_name_plural'
:
'讲师'
},
),
migrations
.
AddField
(
model_name
=
'teacher'
,
name
=
'bad_count'
,
field
=
models
.
IntegerField
(
db_column
=
'sbcount'
,
default
=
0
,
verbose_name
=
'差评数'
),
),
migrations
.
AddField
(
model_name
=
'teacher'
,
name
=
'good_count'
,
field
=
models
.
IntegerField
(
db_column
=
'sgcount'
,
default
=
0
,
verbose_name
=
'好评数'
),
),
migrations
.
AlterField
(
model_name
=
'teacher'
,
name
=
'photo'
,
field
=
models
.
CharField
(
blank
=
True
,
db_column
=
'tphoto'
,
max_length
=
511
,
null
=
True
,
verbose_name
=
'照片'
),
),
]
Day41-55/code/hellodjango/demo/migrations/0002_teacher_photo.py
已删除
100644 → 0
浏览文件 @
b265f0c8
# Generated by Django 2.0.6 on 2018-07-03 03:42
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'demo'
,
'0001_initial'
),
]
operations
=
[
migrations
.
AddField
(
model_name
=
'teacher'
,
name
=
'photo'
,
field
=
models
.
CharField
(
db_column
=
'tphoto'
,
max_length
=
511
,
null
=
True
),
),
]
Day41-55/code/hellodjango/demo/migrations/0003_auto_20180703_1355.py
已删除
100644 → 0
浏览文件 @
b265f0c8
# Generated by Django 2.0.6 on 2018-07-03 05:55
from
django.db
import
migrations
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'demo'
,
'0002_teacher_photo'
),
]
operations
=
[
migrations
.
AlterModelTable
(
name
=
'teacher'
,
table
=
None
,
),
]
Day41-55/code/hellodjango/demo/migrations/0003_auto_20180704_1118.py
0 → 100644
浏览文件 @
4a458a59
# Generated by Django 2.0.6 on 2018-07-04 03:18
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'demo'
,
'0002_auto_20180704_1117'
),
]
operations
=
[
migrations
.
AlterField
(
model_name
=
'teacher'
,
name
=
'bad_count'
,
field
=
models
.
IntegerField
(
db_column
=
'tbcount'
,
default
=
0
,
verbose_name
=
'差评数'
),
),
migrations
.
AlterField
(
model_name
=
'teacher'
,
name
=
'good_count'
,
field
=
models
.
IntegerField
(
db_column
=
'tgcount'
,
default
=
0
,
verbose_name
=
'好评数'
),
),
]
Day41-55/code/hellodjango/demo/migrations/0004_auto_20180703_1412.py
已删除
100644 → 0
浏览文件 @
b265f0c8
# Generated by Django 2.0.6 on 2018-07-03 06:12
from
django.db
import
migrations
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'demo'
,
'0003_auto_20180703_1355'
),
]
operations
=
[
migrations
.
AlterModelOptions
(
name
=
'teacher'
,
options
=
{
'ordering'
:
(
'-no'
,)},
),
migrations
.
AlterModelTable
(
name
=
'teacher'
,
table
=
'tb_teacher'
,
),
]
Day41-55/code/hellodjango/demo/models.py
浏览文件 @
4a458a59
from
django.db
import
models
from
django.db.models
import
PROTECT
# Django框架中包含了ORM(对象关系映射)框架
# ORM可以帮助我们完成对象模型到关系模型的双向转换
class
Subject
(
models
.
Model
):
no
=
models
.
AutoField
(
primary_key
=
True
,
db_column
=
'sno'
,
verbose_name
=
'编号'
)
name
=
models
.
CharField
(
max_length
=
50
,
db_column
=
'sname'
,
verbose_name
=
'学科名称'
)
intro
=
models
.
CharField
(
max_length
=
511
,
db_column
=
'sintro'
,
verbose_name
=
'学科介绍'
)
def
__str__
(
self
):
return
self
.
name
class
Meta
(
object
):
db_table
=
'tb_subject'
verbose_name
=
'学科'
verbose_name_plural
=
'学科'
class
Teacher
(
models
.
Model
):
no
=
models
.
AutoField
(
primary_key
=
True
,
db_column
=
'tno'
,
verbose_name
=
'编号'
)
name
=
models
.
CharField
(
max_length
=
20
,
db_column
=
'tname'
,
verbose_name
=
'姓名'
)
job
=
models
.
CharField
(
max_length
=
10
,
db_column
=
'tjob'
,
verbose_name
=
'职位'
)
intro
=
models
.
CharField
(
max_length
=
1023
,
db_column
=
'tintro'
,
verbose_name
=
'简介'
)
motto
=
models
.
CharField
(
max_length
=
255
,
db_column
=
'tmotto'
,
verbose_name
=
'教学理念'
)
photo
=
models
.
CharField
(
max_length
=
511
,
db_column
=
'tphoto'
,
null
=
True
)
photo
=
models
.
CharField
(
max_length
=
511
,
db_column
=
'tphoto'
,
verbose_name
=
'照片'
,
null
=
True
,
blank
=
True
)
subject
=
models
.
ForeignKey
(
Subject
,
db_column
=
'sno'
,
on_delete
=
PROTECT
,
related_name
=
'+'
,
verbose_name
=
'所属学科'
)
manager
=
models
.
BooleanField
(
default
=
False
,
db_column
=
'tmanager'
,
verbose_name
=
'是否主管'
)
good_count
=
models
.
IntegerField
(
default
=
0
,
db_column
=
'tgcount'
,
verbose_name
=
'好评数'
)
bad_count
=
models
.
IntegerField
(
default
=
0
,
db_column
=
'tbcount'
,
verbose_name
=
'差评数'
)
class
Meta
(
object
):
db_table
=
'tb_teacher'
ordering
=
(
'-no'
,
)
verbose_name
=
'讲师'
verbose_name_plural
=
'讲师'
ordering
=
(
'name'
,
)
Day41-55/code/hellodjango/demo/views.py
浏览文件 @
4a458a59
import
json
from
django.http
import
HttpResponse
from
django.shortcuts
import
render
from
demo.models
import
Teacher
from
demo.models
import
Subject
,
Teacher
def
index
(
request
):
ctx
=
{
'subjects_list'
:
Subject
.
objects
.
all
()}
return
render
(
request
,
'demo/index.html'
,
ctx
)
def
show_teachers
(
request
,
no
):
teachers
=
Teacher
.
objects
.
filter
(
subject__no
=
no
)
ctx
=
{
'teachers_list'
:
teachers
}
return
render
(
request
,
'demo/teacher.html'
,
ctx
)
def
make_good_comment
(
request
,
no
):
teacher
=
Teacher
.
objects
.
get
(
pk
=
no
)
teacher
.
good_count
+=
1
teacher
.
save
()
ctx
=
{
'code'
:
200
,
'result'
:
f
'好评(
{
teacher
.
good_count
}
)'
}
return
HttpResponse
(
json
.
dumps
(
ctx
),
content_type
=
'application/json; charset=utf-8'
)
def
home
(
request
):
# 通过ORM框架实现持久化操作CRUD
ctx
=
{
'teachers_list'
:
list
(
Teacher
.
objects
.
all
())}
return
render
(
request
,
'demo/home.html'
,
ctx
)
def
make_bad_comment
(
request
,
no
):
teacher
=
Teacher
.
objects
.
get
(
pk
=
no
)
teacher
.
bad_count
+=
1
teacher
.
save
()
ctx
=
{
'code'
:
200
,
'result'
:
f
'差评(
{
teacher
.
bad_count
}
)'
}
return
HttpResponse
(
json
.
dumps
(
ctx
),
content_type
=
'application/json; charset=utf-8'
)
Day41-55/code/hellodjango/hellodjango/settings.py
浏览文件 @
4a458a59
...
...
@@ -78,7 +78,7 @@ DATABASES = {
'default'
:
{
'ENGINE'
:
'django.db.backends.mysql'
,
'NAME'
:
'demo'
,
'HOST'
:
'
120.77.222.217
'
,
'HOST'
:
'
localhost
'
,
'PORT'
:
3306
,
'USER'
:
'root'
,
'PASSWORD'
:
'123456'
,
...
...
Day41-55/code/hellodjango/hellodjango/urls.py
浏览文件 @
4a458a59
...
...
@@ -19,6 +19,9 @@ from django.urls import path
from
demo
import
views
urlpatterns
=
[
path
(
''
,
views
.
home
),
path
(
''
,
views
.
index
,
name
=
'index'
),
path
(
'subjects/<int:no>'
,
views
.
show_teachers
),
path
(
'good/<int:no>'
,
views
.
make_good_comment
),
path
(
'bad/<int:no>'
,
views
.
make_bad_comment
),
path
(
'admin/'
,
admin
.
site
.
urls
),
]
Day41-55/code/hellodjango/static/js/jquery.min.js
0 → 100644
浏览文件 @
4a458a59
此差异已折叠。
点击以展开。
Day41-55/code/hellodjango/templates/demo/home.html
已删除
100644 → 0
浏览文件 @
b265f0c8
<!DOCTYPE html>
{% load staticfiles %}
<html
lang=
"en"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
讲师信息
</title>
</head>
<body>
{% for x in teachers_list %}
<h1>
{{ x.name }}老师 - {{ x.job }}
</h1>
<p><strong>
讲师简介
</strong></p>
<p>
{{ x.intro }}
</p>
<p><strong>
教学理念
</strong></p>
<p>
{{ x.motto }}
</p>
<p>
{% if x.photo %}
<img
src=
"{% static x.photo %}"
>
{% endif %}
</p>
<hr>
{% endfor %}
</body>
</html>
\ No newline at end of file
Day41-55/code/hellodjango/templates/demo/index.html
0 → 100644
浏览文件 @
4a458a59
<!DOCTYPE html>
<html
lang=
"en"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
学科信息
</title>
</head>
<body>
<h1>
学科信息
</h1>
<hr>
<ul>
{% for subject in subjects_list %}
<li>
<a
href=
"/subjects/{{ subject.no }}"
>
{{ subject.name }}
</a>
</li>
{% endfor %}
</ul>
</body>
</html>
\ No newline at end of file
Day41-55/code/hellodjango/templates/demo/teacher.html
0 → 100644
浏览文件 @
4a458a59
<!DOCTYPE html>
{% load staticfiles %}
<html
lang=
"en"
>
<head>
<meta
charset=
"UTF-8"
>
<title>
讲师信息
</title>
<style>
.container
{
width
:
960px
;
margin
:
0
auto
;
}
.basic
{
width
:
60%
;
float
:
left
;
}
.potrait
{
width
:
40%
;
float
:
left
;
}
hr
{
clear
:
both
;
}
.button
{
display
:
inline-block
;
width
:
80px
;
height
:
30px
;
background-color
:
red
;
color
:
white
;
font
:
16px
/
30px
Arial
;
text-decoration
:
none
;
text-align
:
center
;
}
</style>
</head>
<body>
<!-- 页面的显示逻辑 -->
{% for x in teachers_list %}
<div
class=
"container"
>
<div
class=
"basic"
>
<h1>
{{ x.name }}老师
</h1>
<p><strong>
讲师简介
</strong></p>
<p>
{{ x.intro }}
</p>
<p><strong>
教学理念
</strong></p>
<p>
{{ x.motto }}
</p>
<a
href=
"/good/{{ x.no }}"
class=
"button"
>
好评({{ x.good_count }})
</a>
<a
href=
"/bad/{{ x.no }}"
class=
"button"
>
差评({{ x.bad_count }})
</a>
</div>
<div
class=
"potrait"
>
{% if x.photo %}
<img
src=
"{% static x.photo %}"
>
{% endif %}
</div>
<hr>
</div>
{% endfor %}
<script
src=
"{% static 'js/jquery.min.js' %}"
></script>
<script>
$
(
function
()
{
$
(
'
.basic .button
'
).
on
(
'
click
'
,
function
(
evt
)
{
evt
.
preventDefault
();
var
a
=
$
(
evt
.
target
)
var
url
=
a
.
attr
(
'
href
'
)
$
.
getJSON
(
url
,
function
(
json
)
{
if
(
json
.
code
==
200
)
{
a
.
text
(
json
.
result
);
}
});
});
});
</script>
</body>
</html>
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录