Skip to content

Commit

Permalink
'更新了Django示例代码'
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfrued committed Jul 6, 2018
1 parent dd4e41f commit 5a46d59
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 46 deletions.
8 changes: 7 additions & 1 deletion Day41-55/code/hellodjango/demo/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from django.contrib import admin

from demo.models import Teacher, Subject
from demo.models import Teacher, Subject, User


class UserAdmin(admin.ModelAdmin):
list_display = ('no', 'username', 'email', 'counter')
ordering = ('no', )


class SubjectAdmin(admin.ModelAdmin):
Expand All @@ -16,4 +21,5 @@ class TeacherAdmin(admin.ModelAdmin):

admin.site.register(Subject, SubjectAdmin)
admin.site.register(Teacher, TeacherAdmin)
admin.site.register(User, UserAdmin)

2 changes: 1 addition & 1 deletion Day41-55/code/hellodjango/demo/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


class DemoConfig(AppConfig):
name = 'demo'
name = '投票'
6 changes: 3 additions & 3 deletions Day41-55/code/hellodjango/demo/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@


class UserForm(forms.ModelForm):
username = forms.CharField(widget=forms.TextInput, min_length=6, max_length=20, help_text='请输入用户名')
password = forms.CharField(widget=forms.PasswordInput, min_length=8, max_length=20, help_text='请输入密码')
email = forms.CharField(widget=forms.EmailInput, max_length=255, help_text='请输入邮箱')
username = forms.CharField(widget=forms.TextInput, min_length=6, max_length=20)
password = forms.CharField(widget=forms.PasswordInput, min_length=8, max_length=20)
email = forms.CharField(widget=forms.EmailInput, max_length=255)

class Meta(object):
model = User
Expand Down
25 changes: 25 additions & 0 deletions Day41-55/code/hellodjango/demo/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 序列化 - 把对象写入数据流 - 串行化 / 归档 / 腌咸菜
# 反序列化 - 从数据流中恢复出对象 - 反串行化 / 解归档
# Python有三个支持序列化的模块
# json - JSON / pickle - 二进制 / shelve
import json
import pickle


class Student(object):

def __init__(self, name, age):
self.name = name
self.age = age


if __name__ == '__main__':
list1 = [10, 'hello', 99.9, 'goodbye']
print(json.dumps(list1))
print(pickle.dumps(list1))
dict1 = {'name': '骆昊', 'age': 38}
print(json.dumps(dict1))
print(pickle.dumps(dict1))
stu = Student('骆昊', 38)
print(pickle.dumps(stu))

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 2.0.6 on 2018-07-06 06:58

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('demo', '0004_auto_20180705_1017'),
]

operations = [
migrations.AlterModelOptions(
name='teacher',
options={'ordering': ('no',), 'verbose_name': '讲师', 'verbose_name_plural': '讲师'},
),
migrations.AddField(
model_name='user',
name='counter',
field=models.IntegerField(default=3, verbose_name='票数'),
),
]
8 changes: 2 additions & 6 deletions Day41-55/code/hellodjango/demo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
from django.db import models
from django.db.models import PROTECT

# 高内聚 低耦合
# 面向对象七个设计原则
# 单一职责原则 / 开闭原则 / 依赖倒转原则 / 里氏替换原则 / 接口隔离原则 / 合成聚合复用原则 / 迪米特法则
# 1995年 - GoF - 23个设计模式
# 创建型模式中的原型模式
proto = sha1()


Expand All @@ -16,6 +11,7 @@ class User(models.Model):
username = models.CharField(max_length=20, unique=True, verbose_name='用户名')
password = models.CharField(max_length=40, verbose_name='口令')
email = models.CharField(max_length=255, verbose_name='邮箱')
counter = models.IntegerField(default=3, verbose_name='票数')

def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
hasher = proto.copy()
Expand Down Expand Up @@ -68,4 +64,4 @@ class Meta(object):
db_table = 'tb_teacher'
verbose_name = '讲师'
verbose_name_plural = '讲师'
ordering = ('name', )
ordering = ('no', )
67 changes: 51 additions & 16 deletions Day41-55/code/hellodjango/demo/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ def login(request):
hasher = proto.copy()
hasher.update(password.encode('utf-8'))
if hasher.hexdigest() == user.password:
request.session['user'] = user
return redirect('sub')
except User.DoesNotExist:
pass
return render(request, 'demo/login.html',
{'hint': '用户名或密码错误'})



def register(request):
form = UserForm()
if request.method.lower() == 'get':
Expand All @@ -44,29 +44,64 @@ def register(request):
return render(request, 'demo/register.html', ctx)


def check_username(request):
ctx = {}
if 'username' in request.GET:
username = request.GET['username']
try:
User.objects.get(username__exact=username)
ctx['valid'] = False
except User.DoesNotExist:
ctx['valid'] = True
return HttpResponse(json.dumps(ctx),
content_type='application/json; charset=utf-8')


def show_subjects(request):
ctx = {'subjects_list': Subject.objects.all()}
return render(request, 'demo/subject.html', ctx)
if 'user' in request.session and request.session['user']:
ctx = {'subjects_list': Subject.objects.all()}
return render(request, 'demo/subject.html', ctx)
else:
return render(request, 'demo/login.html',
{'hint': '请先登录!'})


def show_teachers(request, no):
teachers = Teacher.objects.filter(subject__no=no)
ctx = {'teachers_list': teachers}
return render(request, 'demo/teacher.html', ctx)
if 'user' in request.session and request.session['user']:
teachers = Teacher.objects.filter(subject__no=no)\
.select_related('subject')
ctx = {'teachers_list': teachers}
return render(request, 'demo/teacher.html', ctx)
else:
return render(request, 'demo/login.html',
{'hint': '请先登录!'})


def make_comment(request, no):
ctx = {'code': 200}
try:
teacher = Teacher.objects.get(pk=no)
if request.path.startswith('/good'):
teacher.good_count += 1
ctx['result'] = f'好评({teacher.gcount})'
if 'user' in request.session and request.session['user']:
user = request.session['user']
if user.counter > 0:
try:
teacher = Teacher.objects.get(pk=no)
if request.path.startswith('/good'):
teacher.good_count += 1
ctx['result'] = f'好评({teacher.gcount})'
else:
teacher.bad_count += 1
ctx['result'] = f'差评({teacher.bcount})'
teacher.save()
user.counter -= 1
User.objects.filter(username__exact=user.username)\
.update(counter=user.counter)
request.session['user'] = user
except Teacher.DoesNotExist:
ctx['code'] = 404
else:
teacher.bad_count += 1
ctx['result'] = f'差评({teacher.bcount})'
teacher.save()
except Teacher.DoesNotExist:
ctx['code'] = 404
ctx['code'] = 403
ctx['result'] = '票数不足'
else:
ctx['code'] = 302
ctx['result'] = '请先登录'
return HttpResponse(json.dumps(ctx),
content_type='application/json; charset=utf-8')
24 changes: 20 additions & 4 deletions Day41-55/code/hellodjango/hellodjango/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@

ALLOWED_HOSTS = []

SESSION_EXPIRE_AT_BROWSER_CLOSE = False
SESSION_COOKIE_AGE = 1800

# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
Expand Down Expand Up @@ -73,7 +74,6 @@

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

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
Expand All @@ -88,7 +88,6 @@

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

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
Expand All @@ -107,7 +106,6 @@

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

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/Chongqing'
Expand All @@ -120,10 +118,28 @@

USE_TZ = True

SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer'

# 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/'

# APPEND_SLASH = False

# DEBUG < INFO < WARNING < ERROR < CRITICAL
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': os.getenv('DJANGO_LOG_LEVEL', 'DEBUG'),
},
},
}
1 change: 1 addition & 0 deletions Day41-55/code/hellodjango/hellodjango/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
path('', views.login),
path('login/', views.login),
path('register/', views.register),
path('check/', views.check_username),
path('subjects/', views.show_subjects, name='sub'),
path('subjects/<int:no>/', views.show_teachers),
path('good/<int:no>/', views.make_comment),
Expand Down
3 changes: 3 additions & 0 deletions Day41-55/code/hellodjango/static/images/icon-no.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions Day41-55/code/hellodjango/static/images/icon-yes.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion Day41-55/code/hellodjango/templates/demo/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<title>用户登录</title>
<style>
#login {
width: 250px;
width: 320px;
margin: 20px auto;
}
#login form div {
Expand Down
35 changes: 29 additions & 6 deletions Day41-55/code/hellodjango/templates/demo/register.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<!doctype html>
{% load staticfiles %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户注册</title>
<style>
#reg {
width: 320px;
width: 350px;
margin: 20px auto;
}
#reg form div {
Expand All @@ -24,11 +25,12 @@ <h1>用户注册</h1>
<p class="hint">{{ hint }}</p>
<form action="/register/" method="post">
{% csrf_token %}
<div>用户名: </div>
<div>用户名:</div>
<div>
{{ f.username }}
<span id="uhint"></span>
{% if f.errors.username %}
<span class="hint">用户已被注册</span>
<span class="hint">用户名无效或者已经被注册</span>
{% endif %}
</div>
<div>密码: </div>
Expand All @@ -45,11 +47,32 @@ <h1>用户注册</h1>
<span class="hint">无效的邮箱</span>
{% endif %}
</div>
<div>
<input type="submit" value="注册">
</div>
<input type="submit" value="注册">
</form>
<a href="/">返回登录</a>
</div>
<script src="{% static 'js/jquery.min.js' %}"></script>
<script>
$(function() {
$('#id_username').on('blur', function (evt) {
var $input = $(evt.target);
$.ajax({
'url': '/check/',
'type': 'get',
'data': {'username': $input.val()},
'dataType': 'json',
'success': function(json) {
var $img = $('<img>');
if (json.valid) {
$img.attr('src', '/static/images/icon-yes.svg');
} else {
$img.attr('src', '/static/images/icon-no.svg');
}
$('#uhint').empty().append($img);
}
});
});
});
</script>
</body>
</html>
22 changes: 15 additions & 7 deletions Day41-55/code/hellodjango/templates/demo/subject.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
<head>
<meta charset="UTF-8">
<title>学科信息</title>
<style>
body {
width: 960px;
margin: 0 auto;
}
.sub {
margin: 20px 10px;
}
</style>
</head>
<body>
<h1>学科信息</h1>
<hr>
<ul>
{% for subject in subjects_list %}
<li>
<a href="/subjects/{{ subject.no }}">{{ subject.name }}</a>
</li>
{% endfor %}
</ul>
{% for subject in subjects_list %}
<dl class="sub">
<dt><a href="/subjects/{{ subject.no }}">{{ subject.name }}</a></dt>
<dd>{{ subject.intro }}</dd>
</dl>
{% endfor %}
</body>
</html>
Loading

0 comments on commit 5a46d59

Please sign in to comment.