Django在1.6以前都内置评论系统,用起来也极其方便,但是到了1.8却砍了,怎么才有把它捡回来
step1 pip install django-contrib-comments
[root@hding django]# pip install django-contrib-comments
Downloading/unpacking django-contrib-comments
Downloading django_contrib_comments-1.6.2-py2.py3-none-any.whl (54kB): 54kB downloaded
Requirement already satisfied (use --upgrade to upgrade): Django>=1.6 in /usr/local/lib/python2.7/site-packages (from django-contrib-comments)
Installing collected packages: django-contrib-comments
Successfully installed django-contrib-comments
Cleaning up...
step2 add site_id in settings.py
#/myblog/myblog/settings.py
106 STATICFILES_DIRS = (
107 os.path.join(BASE_DIR, "static"),
108 )
109
110 SITE_ID = 1
step 3 add comments app in settings.py
31 # Application definition
32
33 INSTALLED_APPS = (
34 'django.contrib.admin',
35 'django.contrib.auth',
36 'django.contrib.contenttypes',
37 'django.contrib.sessions',
38 'django.contrib.messages',
39 'django.contrib.staticfiles',
40 'blog',
41 'django_comments', #add comments
42 'django.contrib.sites', #add site
43 )
step 4 评论是当别人看完整篇文章后发出的评价,因此评论系统插在detail.html中
#/myblog/blog/templates/detail.html
1 {% extends "base.html" %}
2 {% block content %}
3 {% load comments %} #把评论表load进来
4 <script type="text/javascript" src="/static/syntaxhighlighter/scripts/shCore.js"></script>
5 <script type="text/javascript" src="/static/syntaxhighlighter/scripts/shBrushPython.js"></script>
6 <link href="/static/syntaxhighlighter/styles/shCoreDefault.css" rel="stylesheet" type="text/css" />
7 <link href="/static/syntaxhighlighter/styles/shThemeDefault.css" rel="stylesheet" type="text/css" />
8 <div class = "posts">
9 <section class="post">
10 <header class="post-header">
11 <h2 class= "post-title">{{blog.title}}<h2>
12 <p class = "post-meta">
13 Time: <a class="post-author" href="#">{{blog.date_time | date:'Y M d'}}</a> 
14 Tag:
15 {% for tag in blog.tags.all %}
16 <a class="post-category" href="#">{{tag.tag_name}}</a>
17 {% endfor %}
18 Count:
19 {% get_comment_count for blog as comment_count %}
20 {{comment_count}}
21
22 </p>
23 </header>
24
25 <div class="post-description">
26 <p>
27 {{blog.content|safe}}
28 </p>
29 </div>
30 </section>
31 <h3>comments</h3>
32 <ul>
33 {% get_comment_list for blog as comment_list %} #获取该博客的评论
34 {% for comment in comment_list %}
35 <li>
36 {{ comment.submit_date }}<br/> #评论提交时间
37 {{comment.comment}}<br/> #评论内容
38 </li>
39 {% endfor %}
40 </ul>
41 <h3>leave a comment</h3>
42 <form action="/blog/sub_comment/" class="pure-form pure-form-stacked" method="post"> #输入评论跳转到后台sub_comment方法进行处理
43 <textarea name="comment_content"></textarea>
44 <input type="hidden" name="blog_id" value={{blog.id}}>
45 <input type="submit" class="pure-button" value="ok">
46 </form>
47
48 <script type="text/javascript">SyntaxHighlighter.all();</script>
49 {% endblock %}
为views.py增加sub_comment方法
import django_comments
...
80 def sub_comment(request):
81 blog_id = request.POST.get('blog_id') #django_comments表中需要blog_id参数
82 comment=request.POST.get('comment_content')
83 django_comments.models.Comment.objects.create(
84 content_type_id=9, #content_type_id=9代表的是博客,8代表tag
85 object_pk=blog_id,
86 site_id=1,
87 user=request.user, #当前登录的用户
88 comment=comment,
89 )
90 return HttpResponseRedirect('/blog/%s' %blog_id)
新增urls.py
1 from django.conf.urls import patterns, include, url
2
3 from django.contrib import admin
4
5 from blog import views
6
7 urlpatterns =[
8 url(r'^$', views.index, name='index'),
9 url(r'^(?P<id>(\d+))/$', views.detail, name='detail'),
10 url(r'^post/$', views.post, name='post'),
11 url(r'^blog_add/$', views.blog_add, name='blog_add'),
12 url(r'^uploadImg/$', views.uploadImg,name='uploadImg'),
13 url(r'^sub_comment/$', views.sub_comment,name='sub_comment'), #新增sub_comment
14 ]
由于新加了应用,整个系统数据库重新同步一下
[root@hding myblog]# python manage.py migrate
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: django_comments, sessions, admin, sites, auth, blog, contenttypes
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying sites.0001_initial... OK
Applying django_comments.0001_initial... OK
Applying django_comments.0002_update_user_email_field_length... OK
进入detail.html会有一个评论留给用户填写,提交后会显示在该页面上
这个评论系统还非常简单,如上传头像还有一些界面的设计都有待优化,但是评论功能已经实现了