Skip to content

Commit 9175339

Browse files
author
yangxg
committed
Step12: comments
1 parent 4d98a07 commit 9175339

File tree

15 files changed

+248
-7
lines changed

15 files changed

+248
-7
lines changed

blog/static/blog/css/custom.css

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ figure {
200200
padding-bottom: 4em;
201201
}
202202

203-
.post {
203+
.post, .comment-area {
204204
background: #fff;
205205
padding: 30px 30px 0;
206206
}
@@ -647,7 +647,7 @@ figure {
647647
* 10.0 - Contact Form
648648
*/
649649

650-
.contact-form input {
650+
.contact-form input, .comment-form input {
651651
border: 1px solid #aaa;
652652
margin-bottom: 15px;
653653
width: 100%;
@@ -657,7 +657,7 @@ figure {
657657
transition: 0.4s border-color linear;
658658
}
659659

660-
.contact-form textarea {
660+
.contact-form textarea, .comment-form textarea {
661661
border: 1px solid #aaa;
662662
margin-bottom: 15px;
663663
width: 100%;
@@ -668,8 +668,8 @@ figure {
668668
transition: 0.4s border-color linear;
669669
}
670670

671-
.contact-form input:focus,
672-
.contact-form textarea:focus {
671+
.contact-form input:focus, .comment-form input:focus,
672+
.contact-form textarea:focus, .comment-form textarea:focus {
673673
border-color: #666;
674674
}
675675

@@ -798,4 +798,31 @@ figure {
798798
.overlay ul li {
799799
min-height: 34px;
800800
}
801-
}
801+
}
802+
803+
/* Comment list */
804+
.comment-list {
805+
margin-top: 30px;
806+
font-size: 16px;
807+
}
808+
809+
.comment-item {
810+
border-bottom: 1px #ccc solid;
811+
margin-bottom: 20px;
812+
padding-bottom: 20px;
813+
}
814+
815+
.comment-item .name,
816+
.comment-item .date {
817+
color: #444;
818+
font-size: 14px;
819+
}
820+
821+
.comment-item .name:after {
822+
content: '·';
823+
}
824+
825+
.comment-item .text {
826+
padding-top: 5px;
827+
}
828+

blog/views.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from django.shortcuts import render, get_object_or_404
44

5+
from comments.forms import CommentForm
56
from .models import Post, Category
67

78
"""
@@ -25,6 +26,8 @@ def index(request):
2526
return render(request, 'blog/index.html', context={'post_list': post_list})
2627

2728

29+
"""
30+
增加了评论功能后需要相应地更新 detail 函数,更新后的函数见下边。
2831
def detail(request, pk):
2932
post = get_object_or_404(Post, pk=pk)
3033
post.body = markdown.markdown(post.body,
@@ -35,6 +38,25 @@ def detail(request, pk):
3538
])
3639
return render(request, 'blog/detail.html', context={'post': post})
3740
41+
"""
42+
43+
44+
def detail(request, pk):
45+
post = get_object_or_404(Post, pk=pk)
46+
post.body = markdown.markdown(post.body,
47+
extensions=[
48+
'markdown.extensions.extra',
49+
'markdown.extensions.codehilite',
50+
'markdown.extensions.toc',
51+
])
52+
form = CommentForm()
53+
comment_list = post.comment_set.all()
54+
context = {'post': post,
55+
'form': form,
56+
'comment_list': comment_list
57+
}
58+
return render(request, 'blog/detail.html', context=context)
59+
3860

3961
def archives(request, year, month):
4062
post_list = Post.objects.filter(created_time__year=year, created_time__month=month)

blogproject/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
'django.contrib.messages',
3737
'django.contrib.staticfiles',
3838
'blog',
39+
'comments',
3940
]
4041

4142
MIDDLEWARE = [

blogproject/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818

1919
urlpatterns = [
2020
url(r'^admin/', admin.site.urls),
21-
url(r'', include('blog.urls'))
21+
url(r'', include('blog.urls')),
22+
url(r'', include('comments.urls')),
2223
]

comments/__init__.py

Whitespace-only changes.

comments/admin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

comments/apps.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class CommentsConfig(AppConfig):
5+
name = 'comments'

comments/forms.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from django import forms
2+
3+
from .models import Comment
4+
5+
6+
class CommentForm(forms.ModelForm):
7+
class Meta:
8+
model = Comment
9+
fields = ['name', 'email', 'url', 'text']
10+
11+
widgets = {
12+
'name': forms.TextInput(attrs={
13+
'placeholder': "名字",
14+
}),
15+
'email': forms.TextInput(attrs={
16+
'placeholder': "邮箱",
17+
}),
18+
'url': forms.TextInput(attrs={
19+
'placeholder': "网址",
20+
}),
21+
}

comments/migrations/0001_initial.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.10.6 on 2017-04-08 11:38
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
import django.db.models.deletion
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
initial = True
12+
13+
dependencies = [
14+
('blog', '0001_initial'),
15+
]
16+
17+
operations = [
18+
migrations.CreateModel(
19+
name='Comment',
20+
fields=[
21+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
22+
('name', models.CharField(max_length=100)),
23+
('email', models.EmailField(max_length=255)),
24+
('url', models.URLField(blank=True)),
25+
('text', models.TextField()),
26+
('created_time', models.DateTimeField(auto_now_add=True)),
27+
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='blog.Post')),
28+
],
29+
),
30+
]

comments/migrations/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)