Skip to content

Commit aaee8c7

Browse files
committed
V0.85
1 parent eac51a7 commit aaee8c7

32 files changed

+404
-114
lines changed

.idea/workspace.xml

+204-84
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
15 Bytes
Binary file not shown.
35 Bytes
Binary file not shown.

Station/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
'django.contrib.staticfiles',
4040
'news',
4141
'haystack',
42+
'comments',
4243
]
4344

4445
MIDDLEWARE = [

Station/urls.py

+1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020
url(r'^admin/', admin.site.urls),
2121
url(r'', include('news.urls')),
2222
url(r'^search/', include('haystack.urls')),
23+
url(r'', include('comments.urls')),
2324
]

comments/__init__.py

Whitespace-only changes.
126 Bytes
Binary file not shown.
171 Bytes
Binary file not shown.
606 Bytes
Binary file not shown.
841 Bytes
Binary file not shown.
341 Bytes
Binary file not shown.
860 Bytes
Binary file not shown.

comments/admin.py

+3
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

+5
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

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- coding:utf-8 -*-
2+
# made in ly
3+
from django import forms
4+
from .models import Comment
5+
6+
7+
class CommentForm(forms.ModelForm):
8+
class Meta:
9+
model = Comment
10+
fields = ['name', 'email', 'url', 'text']

comments/migrations/0001_initial.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.10 on 2018-05-26 09:09
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+
('news', '0002_remove_illustration_imgname'),
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='news.Post')),
28+
],
29+
),
30+
]

comments/migrations/__init__.py

Whitespace-only changes.
Binary file not shown.
Binary file not shown.

comments/models.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
from django.db import models
3+
from django.utils.six import python_2_unicode_compatible
4+
5+
# python_2_unicode_compatible 装饰器用于兼容 Python2
6+
@python_2_unicode_compatible
7+
class Comment(models.Model):
8+
name = models.CharField(max_length=100)
9+
email = models.EmailField(max_length=255)
10+
url = models.URLField(blank=True)
11+
text = models.TextField()
12+
created_time = models.DateTimeField(auto_now_add=True)
13+
14+
post = models.ForeignKey('news.Post')
15+
16+
def __str__(self):
17+
return self.text[:20]

comments/tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

comments/urls.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- coding:utf-8 -*-
2+
# made in ly
3+
from django.conf.urls import url
4+
5+
from . import views
6+
7+
app_name = 'comments'
8+
9+
urlpatterns = [
10+
url(r'^comment/post/(?P<post_pk>[0-9]+)/$', views.post_comment, name='post_comment'),
11+
]

comments/views.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from django.shortcuts import render, get_object_or_404, redirect
2+
from news.models import Post
3+
4+
from .models import Comment
5+
from .forms import CommentForm
6+
7+
8+
def post_comment(request, post_pk):
9+
post = get_object_or_404(Post, pk=post_pk)
10+
11+
if request.method == 'POST':
12+
form = CommentForm(request.POST)
13+
14+
if form.is_valid():
15+
comment = form.save(commit=False)
16+
17+
# 将评论和被评论的文章关联起来。
18+
comment.post = post
19+
20+
comment.save()
21+
22+
return redirect(post)
23+
24+
else:
25+
26+
comment_list = post.comment_set.all()
27+
context = {'post': post,
28+
'form': form,
29+
'comment_list': comment_list
30+
}
31+
return render(request, 'news/post_detail.html', context=context)
32+
# 不是 post 请求,说明用户没有提交数据,重定向到文章详情页。
33+
return redirect(post)

news/__pycache__/views.cpython-35.pyc

363 Bytes
Binary file not shown.

news/static/news/css/index.css

+17-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

news/views.py

+16-24
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from django.shortcuts import render, get_object_or_404
33
import markdown
44
# Create your views here.
5-
5+
from comments.forms import CommentForm
66
from django.http import HttpResponse
77
from .models import Post, Category, Tag, Illustration
88
from django.views.generic import ListView, DetailView
@@ -125,29 +125,21 @@ def get_object(self, queryset=None):
125125
post.toc = md.toc
126126
return post
127127

128-
# def get_context_data(self, **kwargs):
129-
# # 覆盖get_context_data 的目的是为了除了将post传递给模板外(DateilView已经帮我们完成)
130-
# # 还要把评论表单、post下评论的列表传递给模板
131-
# context = super(PostDetailView, self).get_context_data(**kwargs)
132-
# form = CommentForm()
133-
# comment_list = self.object.comment_set.all()
134-
# context.update(
135-
# {
136-
# 'form': form,
137-
# 'comment_list': comment_list
138-
# }
139-
# )
140-
# return context
141-
142-
143-
# class CategoryView(ListView):
144-
# model = Post
145-
# template_name = 'news/index.html'
146-
# context_object_name = 'post_list'
147-
#
148-
# def get_queryset(self):
149-
# cate = get_object_or_404(Category, pk=self.kwargs.get('pk'))
150-
# return super(CategoryView, self).get_queryset().filter(category=cate)
128+
def get_context_data(self, **kwargs):
129+
# 覆盖get_context_data 的目的是为了除了将post传递给模板外(DateilView已经帮我们完成)
130+
# 还要把评论表单、post下评论的列表传递给模板
131+
context = super(PostDetailView, self).get_context_data(**kwargs)
132+
form = CommentForm()
133+
comment_list = self.object.comment_set.all()
134+
context.update(
135+
{
136+
'form': form,
137+
'comment_list': comment_list
138+
}
139+
)
140+
return context
141+
142+
151143

152144
def category(request, pk):
153145
# 记得在开始部分导入 Category 类

readme.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ passwd:news123456789
33

44
V0.7 û������ϵͳ��ȱ���������ܣ�ȱ�������б�����
55

6-
V0.8 ��0.7�Ļ��������ӿ��������ܡ�������Ȼ���ڵ�������û������ϵͳ��ȱ�������б����档
6+
V0.8 ��0.7�Ļ��������ӿ��������ܡ�������Ȼ���ڵ�������û������ϵͳ��ȱ�������б����档
7+
8+
v0.85 ��V0.8�Ļ���������������ϵͳ���������۱�������ʽ̫�ѿ�

templates/base.html

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<script type="text/javascript" src="{% static 'news/js/jquery-1.4.2.min.js' %}" ></script>
1313
<script type="text/javascript" src="{% static 'news/js/jquery.cycle.all.js' %}"></script>
1414
<script type="text/javascript" src="{% static 'news/js/jquery-ui-1.8.5.custom.min.js' %}"></script>
15+
1516
<!--[if lt IE 9]>
1617
<script type="text/javascript" src="{% static 'news/js/html5.js' %}"></script>
1718
<![endif]-->
@@ -20,6 +21,7 @@
2021
color: red;
2122
}
2223
</style>
24+
2325
</head>
2426

2527
<body>

templates/news/post_detail.html

+48-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,58 @@
44
<article class="post post-1">
55
<div class="top">
66
<div class="container">
7-
<h2>{{ post.title }}</h2>
7+
<h2>{{ post.title }}</h2>
88
<p>发布时间:{{ post.created_time }}</p>
9-
<p>{{ post.body|safe }}</p>
9+
<p>{{ post.body|safe }}</p>
1010

11+
<form action="{% url 'comments:post_comment' post.pk %}" method="post" class="comment-form">
12+
{% csrf_token %}
13+
<div class="">
14+
<div class="">
15+
<label for="{{ form.name.id_for_label }}">名字:</label>
16+
{{ form.name }}
17+
{{ form.name.errors }}
1118

12-
</div>
19+
20+
<label for="{{ form.email.id_for_label }}">邮箱:</label>
21+
{{ form.email }}
22+
{{ form.email.errors }}
23+
24+
<label for="{{ form.url.id_for_label }}">URL:</label>
25+
{{ form.url }}
26+
{{ form.url.errors }}
27+
</div>
28+
<div class="">
29+
<label for="{{ form.text.id_for_label }}" >评论:</label>
30+
{{ form.text }}
31+
{{ form.text.errors }}
32+
<br>
33+
<button type="submit">发表</button>
34+
35+
</div>
36+
</div> <!-- row -->
37+
</form>
38+
39+
<div>
40+
<ul class="comment-list list-unstyled">
41+
<h3>评论列表,共 <span>{{ post.comment_set.count }}</span> 条评论</h3>
42+
{% for comment in comment_list %}
43+
<li class="comment-item">
44+
<span class="nickname">{{ comment.name }}</span>
45+
<time class="submit-date">{{ comment.created_time }}</time>
46+
<div class="text">
47+
{{ comment.text }}
48+
</div>
49+
</li>
50+
{% empty %}
51+
暂无评论
52+
{% endfor %}
53+
</ul>
54+
</div>
55+
56+
</div>
1357
</div>
1458
</article>
15-
</section>
59+
</section>
1660

1761
{% endblock %}
Binary file not shown.

whoosh_index/_MAIN_141.toc

2.08 KB
Binary file not shown.

whoosh_index/_MAIN_6.toc

-2.08 KB
Binary file not shown.

0 commit comments

Comments
 (0)