Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Python-100-Days
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
huangkq
Python-100-Days
Commits
4a458a59
Commit
4a458a59
authored
Jul 04, 2018
by
jackfrued
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
'更新了Django示例代码'
parent
b265f0c8
Changes
16
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
239 additions
and
99 deletions
+239
-99
db.sqlite3
Day41-55/code/hellodjango/db.sqlite3
+0
-0
admin.py
Day41-55/code/hellodjango/demo/admin.py
+9
-2
0001_initial.py
Day41-55/code/hellodjango/demo/migrations/0001_initial.py
+21
-6
0002_auto_20180704_1117.py
...de/hellodjango/demo/migrations/0002_auto_20180704_1117.py
+36
-0
0002_teacher_photo.py
...55/code/hellodjango/demo/migrations/0002_teacher_photo.py
+0
-18
0003_auto_20180703_1355.py
...de/hellodjango/demo/migrations/0003_auto_20180703_1355.py
+0
-17
0003_auto_20180704_1118.py
...de/hellodjango/demo/migrations/0003_auto_20180704_1118.py
+23
-0
0004_auto_20180703_1412.py
...de/hellodjango/demo/migrations/0004_auto_20180703_1412.py
+0
-21
models.py
Day41-55/code/hellodjango/demo/models.py
+22
-5
views.py
Day41-55/code/hellodjango/demo/views.py
+31
-5
settings.py
Day41-55/code/hellodjango/hellodjango/settings.py
+1
-1
urls.py
Day41-55/code/hellodjango/hellodjango/urls.py
+4
-1
jquery.min.js
Day41-55/code/hellodjango/static/js/jquery.min.js
+2
-0
home.html
Day41-55/code/hellodjango/templates/demo/home.html
+0
-23
index.html
Day41-55/code/hellodjango/templates/demo/index.html
+18
-0
teacher.html
Day41-55/code/hellodjango/templates/demo/teacher.html
+72
-0
No files found.
Day41-55/code/hellodjango/db.sqlite3
deleted
100644 → 0
View file @
b265f0c8
Day41-55/code/hellodjango/demo/admin.py
View file @
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
View file @
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
View file @
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
deleted
100644 → 0
View file @
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
deleted
100644 → 0
View file @
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
View file @
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
deleted
100644 → 0
View file @
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
View file @
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
View file @
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
View file @
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
View file @
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
View file @
4a458a59
This diff is collapsed.
Click to expand it.
Day41-55/code/hellodjango/templates/demo/home.html
deleted
100644 → 0
View file @
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
View file @
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
View file @
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment