'更新了Django示例代码'
parent
b265f0c833
commit
4a458a594e
|
@ -1,12 +1,19 @@
|
|||
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)
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
# Generated by Django 2.0.6 on 2018-07-03 02:20
|
||||
# Generated by Django 2.0.6 on 2018-07-04 02:30
|
||||
|
||||
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',),
|
||||
},
|
||||
),
|
||||
]
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
# 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='照片'),
|
||||
),
|
||||
]
|
|
@ -1,18 +0,0 @@
|
|||
# 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),
|
||||
),
|
||||
]
|
|
@ -1,17 +0,0 @@
|
|||
# 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,
|
||||
),
|
||||
]
|
|
@ -0,0 +1,23 @@
|
|||
# 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='好评数'),
|
||||
),
|
||||
]
|
|
@ -1,21 +0,0 @@
|
|||
# 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',
|
||||
),
|
||||
]
|
|
@ -1,17 +1,34 @@
|
|||
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', )
|
||||
|
|
|
@ -1,9 +1,35 @@
|
|||
import json
|
||||
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import render
|
||||
|
||||
from demo.models import Teacher
|
||||
from demo.models import Subject, Teacher
|
||||
|
||||
|
||||
def home(request):
|
||||
# 通过ORM框架实现持久化操作CRUD
|
||||
ctx = {'teachers_list': list(Teacher.objects.all())}
|
||||
return render(request, 'demo/home.html', ctx)
|
||||
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 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')
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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),
|
||||
]
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1,23 +0,0 @@
|
|||
<!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>
|
|
@ -0,0 +1,18 @@
|
|||
<!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>
|
|
@ -0,0 +1,72 @@
|
|||
<!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>
|
Loading…
Reference in New Issue