6.1.comments插件

(1)安装

pip install django-contrib-comments

(02)settings

INSTALLED_APPS = [
          'django.contrib.sites',
          'django_comments',
]
SITE_ID =1

(3)website/url

 url(r'^comments/', include('django_comments.urls')),

(4)修改源码

django_comments/abstracts.py第36行

原代码

    site = models.ForeignKey(Site, on_delete=models.CASCADE)

修改后

 site = models.ForeignKey(Site,default=None,blank=True,null=True,on_delete=models.CASCADE)

(5)修改源码

django_comments/abstracts.py第52行

修改和增加了几个字段

class CommentAbstractModel(BaseCommentAbstractModel):
    """
    A user comment about some object.
    """

    # Who posted this comment? If ``user`` is set then it was an authenticated
    # user; otherwise at least user_name should have been set and the comment
    # was posted by a non-authenticated user.
    user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('user'),
                             blank=True, null=True, related_name="%(class)s_comments",
                             on_delete=models.SET_NULL)

    # Explicit `max_length` to apply both to Django 1.7 and 1.8+.
    user_email = models.EmailField(_("user's email address"), max_length=254,
                                   blank=True,null=True,default='xxx@xxx.com')
    user_url = models.URLField(_("user's URL"), blank=True,null=True,default='https://xxx.xxx.xxx.xxx')

    user_name = models.CharField(_("user's name"), max_length=50, blank=True)
    user_img = models.ImageField(upload_to="user_images", blank=True,null=True,verbose_name="用户图像")
    comment_title = models.CharField(max_length=256,default="无标题", blank=True,null=True,verbose_name="评论标题")
    parent_comment = models.ForeignKey('self',default=None, blank=True,null=True,related_name="child_comment",on_delete=models.CASCADE)     level = models.PositiveIntegerField(default=0, blank=True,null=True,verbose_name='评论层级') comment = models.TextField(_('comment'), max_length=COMMENT_MAX_LENGTH)    .     .     .     .

(6)修改源码

django_comments/admin.py第29和37行

_('Content'),
            {'fields': ('user', 'user_name', 'user_email', 'user_url', 'comment','comment_title','parent_comment')}
list_display = ('comment_title','name', 'content_type', 'object_pk', 'ip_address', 'submit_date', 'is_public', 'is_removed')

(7)修改源码

django_comments/forms.py第97行表单

    class CommentDetailsForm(CommentSecurityForm):        """    Handles the specific details of the comment (name, comment, etc.).
      """
    # name = forms.CharField(label=pgettext_lazy("Person name", "Name"), max_length=50)
    # email = forms.EmailField(label=_("Email address"))
    # url = forms.URLField(label=_("URL"), required=False)
    # Translators: 'Comment' is a noun here.
    comment = forms.CharField(label=_('Comment'), widget=forms.Textarea,
                              max_length=COMMENT_MAX_LENGTH)
    comment_title = forms.CharField(max_length=256,label=_('标题'))
    parent_id = forms.IntegerField(min_value=-1,label=_('父评论的id'))
    level = forms.IntegerField(min_value=0,label=_('层级'),)  .  .  . 
    def get_comment_create_data(self, site_id=None):
        """
        Returns the dict of data to be used to create a comment. Subclasses in
        custom comment apps that override get_comment_model can override this
        method to add extra fields onto a custom comment model.
        """
        parent_id = self.cleaned_data['parent_id']
        if parent_id <= 0:
            parent = None
            _level = self.cleaned_data['level']
        else:
            parent = get_model().objects.get(id=parent_id)
            _level = self.cleaned_data['level'] + 4    # 4是缩进

        return dict(
            content_type=ContentType.objects.get_for_model(self.target_object),
            object_pk=force_text(self.target_object._get_pk_val()),
            # user_name=self.cleaned_data["name"],
            # user_email=self.cleaned_data["email"],
            # user_url=self.cleaned_data["url"],
            comment_title = self.cleaned_data['comment_title'],
            parent_comment = parent,
            level = _level,
            comment=self.cleaned_data["comment"],
            submit_date=timezone.now(),
            site_id=site_id or getattr(settings, "SITE_ID", None),
            is_public=True,
            is_removed=False,
        )

(8)修改源码

django_comments/views/comments.py第97行表单

52行

  # 添加
    if not request.session.get('login', None) and not user_is_authenticated:
        return redirect("/")

116行

   # if user_is_authenticated:
    #     comment.user = request.user
    #添加
    if request.session.get('login',None):
        comment.user_name = request.session['screen_name']
        comment.user_img = request.session['profile_image_url']

以上都修改完后

python manage.py makemigtations

python manage.py migrate

6.2.评论表单

detail.html

          <div class="row">
                    <hr />
                    {% get_comment_form for entry as form %}
                    {% get_comment_count for entry  as comment_count %}
                    <h3 >评论总数: {{ comment_count }}</h3>
                    <hr />
                </div>

                <div class="row">
                    <form class="form-horizontal" action="{% comment_form_target %}" method="post">
                        {% csrf_token %}
                        <div class="form-group">
                            <label for="input_title" class="pull-left control-label">评论标题:</label>
                            <div class="col-sm-6">
                                <input class="form-control" name="comment_title" id="input_title" placeholder="请输入标题" required />
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="input_comment" class="pull-left control-label">评论内容:</label>
                            <div class="col-sm-6">
                            <textarea style="resize:none;" class="form-control" rows=6 name="comment" id="input_comment" placeholder="在此输入评论" required></textarea>
                            </div>
                        </div>
                        <span style="display: none;">{{ form.honeypot }}</span>
                        {{ form.content_type }}
                        {{ form.object_pk }}
                        {{ form.timestamp }}
                        {{ form.security_hash }}
                        <input type="hidden" name="next" value="{% url 'blog:blog_detail' entry.id %}" />
                        <input  name="parent_id" type="hidden" value="-1" />
                        <input  name=" />

                        <div class="form-group col-sm-7">
                            <div class="pull-left" style="margin-left: 68px;">
                                <button type="reset"  class="btn btn-default"><span class="glyphicon glyphicon-repeat"></span>&nbsp;&nbsp;重置</button>
                            </div>
                            <div class="pull-right" style="margin-right: 12px;">
                                <button type="submit" class="btn btn-success" id="id_submit"><span class="glyphicon glyphicon-send"></span>&nbsp;&nbsp;评论</button>
                            </div>
                        </div>
                    </form>
                </div>

效果:

现在测试评论,可以添加成功,但是还不能显示

6.3.显示评论

(1)views.py

from django_comments.models import Comment

def detail(request,blog_id):
    # entry = models.Entry.objects.get(id=blog_id)
    entry = get_object_or_404(models.Entry,id=blog_id)

    md = markdown.Markdown(extensions=[
        'markdown.extensions.extra',
        'markdown.extensions.codehilite',
        'markdown.extensions.toc',
    ])
    entry.body = md.convert(entry.body)
    entry.toc = md.toc
    entry.increase_visiting()

    comment_list = list()

    def get_comment_list(comments):
        for comment in comments:
            comment_list.append(comment)
            children = comment.child_comment.all()
            if len(children) > 0:
                get_comment_list(children)

    top_comments = Comment.objects.filter(object_pk=blog_id, parent_comment=None,
                                          content_type__app_label='blog').order_by('-submit_date')

    get_comment_list(top_comments)
    return render(request, 'blog/detail.html', locals())

(2)detail.html

<div class="row">
                    <hr />
                    {% get_comment_form for entry as form %}
                    {% get_comment_count for entry  as comment_count %}
                    <h3 >评论总数: {{ comment_count }}</h3>
                    <hr />
                </div>

              {#            评论表单#}
            {% if request.session.login or request.user.is_authenticated %}
                <div class="row">
                    <form class="form-horizontal" action="{% comment_form_target %}" method="post">
                        {% csrf_token %}
                        <div class="form-group">
                            <label for="input_title" class="pull-left control-label">评论标题:</label>
                            <div class="col-sm-6">
                                <input class="form-control" name="comment_title" id="input_title" placeholder="请输入标题" required />
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="input_comment" class="pull-left control-label">评论内容:</label>
                            <div class="col-sm-6">
                            <textarea style="resize:none;" class="form-control" rows=6 name="comment" id="input_comment" placeholder="在此输入评论" required></textarea>
                            </div>
                        </div>
                        <span style="display: none;">{{ form.honeypot }}</span>
                        {{ form.content_type }}
                        {{ form.object_pk }}
                        {{ form.timestamp }}
                        {{ form.security_hash }}
                        <input type="hidden" name="next" value="{% url 'blog:blog_detail' entry.id %}" />
                        <input  name="parent_id" type="hidden" value="-1" />
                        <input  name="level" type="hidden" value="0" />

                        <div class="form-group col-sm-7">
                            <div class="pull-left" style="margin-left: 68px;">
                                <button type="reset"  class="btn btn-default"><span class="glyphicon glyphicon-repeat"></span>&nbsp;&nbsp;重置</button>
                            </div>
                            <div class="pull-right" style="margin-right: 12px;">
                                <button type="submit" class="btn btn-success" id="id_submit"><span class="glyphicon glyphicon-send"></span>&nbsp;&nbsp;评论</button>
                            </div>
                        </div>

                    </form>
                </div>
            {% else %}
                <h3>登陆后才可以评论</h3>
            {% endif %}
            <hr />

{#            评论显示区#}
            <div class="row">
            {% for comment in comment_list %}
                <div class="single_comment" style="margin-left: {{ comment.level }}em">
                    <div>
                        {% if comment.user_img %}
                            <img src="{{ comment.user_img }}" alt="user_image" />
                        {% else %}
                            <img src="{% static 'blog/images/cl.jpg' %}" />
                        {% endif %}
                    </div>
                    <div class="col-md-11 comment-content" style="margin-bottom: 10px;">
                        <strong>{{ comment.comment_title }}</strong>
                        <div>
                            {% if comment.parent_comment %}
                                        {{ comment.user_name }}{{ request.user }}
                                        <i class="glyphicon glyphicon-share-alt"></i>
                                        {{ comment.parent_comment.user_name }}{{ request.user }}
                            {% else %}
                                    By&nbsp;&nbsp;
                                        {{ comment.user_name }}{{ request.user }}
                                    &nbsp;&nbsp;&nbsp;On&nbsp;&nbsp;
                            {% endif %}
                            <em>{{ comment.submit_date }}</em>
                            {% if request.session.login or request.user.is_authenticated %}
                            &nbsp;&nbsp;&nbsp;<a href="#">回复</a>
                            {% endif %}
                        </div>
                    <br />
                        <p>{{ comment.comment }}</p>
                    </div>
                </div>
            {% endfor %}
            </div>
            </div>

(3)static/blog/images/cl.jpg

设置默认图片(如果没有登录就显示默认图片,为了测试评论)

(4)blog/blog_comment.css

hr {
    border-top: 1px solid lightgrey;
    border-bottom: 1px solid #fff;
}

.single_comment {
    margin-bottom: 20px;
    font-family: "Microsoft Yahei","微软雅黑",Arial,Helvetica,STHeiti,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";
}

.single_comment img {
    width:50px;
    height:50px;
    border-radius:50%;
    overflow:hidden;
    float: left;
    margin-left: 10px;
    margin-top: 5px;
}

.single_comment p {
    margin-bottom:;
}

.comment-content {
    border-bottom: 1px dashed lightgrey;
    border-left: 1px dashed #2d7da4;
    padding-bottom: 5px;
}

detail.html引用

{% block css %}
    <link rel="stylesheet" href="{% static 'blog/css/github.css' %}">
    <link rel="stylesheet" href="{% static 'blog/css/blog_comment.css' %}">
{% endblock %}

效果:

6.4.评论回复

(1)blog/urls.py

  url(r'^reply/(?P<comment_id>\d+)/$', views.reply, name='comment_reply'),

(2)views.py

from django_comments import models as comment_models

def reply(request, comment_id):
    if not request.session.get('login', None) and not request.user.is_authenticated():
        return redirect('/')
    parent_comment = get_object_or_404(comment_models.Comment, id=comment_id)
    return render(request, 'blog/reply.html', locals())

(3)blog/reply.html

{% extends 'blog/base.html' %}
{% load comments %}
{% load static %}
{% block title %}回复评论{% endblock %}
{% block css %}

    <link rel="stylesheet" href="{% static 'blog/css/blog_comment.css' %}">
{% endblock %}

{% block content %}
    <div class="container-fluid" style="margin: 30px 50px 0 50px;min-height: 450px;">
        <div class="row">
            <div class="single_comment">
                <div>
                    {% if request.session.login %}
                    <img src="{{ parent_comment.user_img }}" alt="user_img" />
                    {% else %}
                    <img src="{% static 'blog/images/cl.jpg' %}" alt="admin_img" />
                    {% endif %}
                </div>
            <div class="col-md-11">
                <strong>{{ parent_comment.comment_title }}</strong>
                <div class="small">
                    <strong>{{ parent_comment.username }} {{ request.user }}</strong>
                    <em>{{ parent_comment.submit_date }}</em>
                </div>
                <br />
                <p>{{ parent_comment.comment }}</p>
            </div>

            </div>
        </div>
            <br />
        {% if request.session.login or request.user.is_authenticated %}
            <div class="row" style="margin-left: 30px;">
            <h3>回复&nbsp;&nbsp;&nbsp;{{ parent_comment.username }}{{ request.user }}&nbsp;&nbsp;&nbsp;的评论:</h3>
            {% get_comment_form for parent_comment.content_object as form %}
                    <form class="form-horizontal" action="{% comment_form_target %}" method="post">
                        {% csrf_token %}
                        <div class="form-group">
                            <label for="input_title" class="pull-left control-label">评论标题:</label>
                            <div class="col-sm-6">
                                <input class="form-control" name="comment_title" id="input_title" placeholder="请输入标题" required />
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="input_comment" class="pull-left control-label">评论内容:</label>
                            <div class="col-sm-6">
                            <textarea style="resize:none;" class="form-control" rows=6 name="comment" id="input_comment" placeholder="在此输入评论" required></textarea>
                            </div>
                        </div>
                        <span style="display: none;">{{ form.honeypot }}</span>
                        {{ form.content_type }}
                        {{ form.object_pk }}
                        {{ form.timestamp }}
                        {{ form.security_hash }}

                        <input type="hidden" name="next" value="{% url 'blog:blog_detail' parent_comment.content_object.id %}" />
                        <input  name="parent_id" type="hidden" value="{{ parent_comment.id }}" />
                        <input  name="level" type="hidden" value="{{ parent_comment.level }}" />

                        <div class="form-group col-sm-7">
                            <div class="pull-left" style="margin-left: 68px;">
                                <button type="reset"  class="btn btn-default"><span class="glyphicon glyphicon-repeat"></span>&nbsp;&nbsp;重置</button>
                            </div>
                            <div class="pull-right" style="margin-right: 12px;">
                                <button type="submit" class="btn btn-success" id="id_submit"><span class="glyphicon glyphicon-send"></span>&nbsp;&nbsp;评论</button>
                            </div>
                        </div>

                    </form>
                </div>
        {% else %}
            <h3>登陆后才可以评论</h3>
        {% endif %}
        <br />

    <div>
        <a href="{% url 'blog:blog_detail' parent_comment.content_object.id %}">暂时不评论,返回先前页面!</a>
    </div>

    </div>
{% endblock %}

(4)detail.html

<a href="{% url 'blog:comment_reply' comment.id %}">回复</a>

测试回复功能如下:

Django+Bootstrap+Mysql 搭建个人博客(六)的更多相关文章

  1. Django+Bootstrap+Mysql 搭建个人博客(一)

    1.1.环境搭建 (1)虚拟环境 mkvirtualenv website pip install django==1.11.7 (2)创建项目和app:website和blog (3)设置中文set ...

  2. Django+Bootstrap+Mysql 搭建个人博客(五)

    5.1.自定义403,404和500页面 (1)website/urls.py from blog import views as blog_views handler403 = blog_views ...

  3. Django+Bootstrap+Mysql 搭建个人博客(四)

    4.1.博客分类 (1)blog_tags.py @register.simple_tag def get_categories(): return Category.objects.all() @r ...

  4. Django+Bootstrap+Mysql 搭建个人博客(三)

    3.1.分页功能 (1)views.py from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger def make ...

  5. Django+Bootstrap+Mysql 搭建个人博客(二)

    2.1.博客首页设计 (1)settings.py MEDIA_ROOT = os.path.join(BASE_DIR,'media').replace("//","/ ...

  6. Django+Bootstrap+Mysql 搭建个人博客 (六)

    6.1.comments插件 (1)安装 pip install django-contrib-comments (02)settings INSTALLED_APPS = [ 'django.con ...

  7. Python Web开发:Django+BootStrap实现简单的博客项目

    创建blog的项目结构 关于如何创建一个Django项目,请查看[Python Web开发:使用Django框架创建HolleWorld项目] 创建blog的数据模型 创建一个文章类 所有开发都是数据 ...

  8. django入门--django-blog-zinnia搭建个人博客

    1.安装python 选择合适python2.7及以上版本安装https://www.python.org/downloads/ 2.建立虚拟环境 这不是必须的,但是建议使用,为每个项目单独引入依赖, ...

  9. Django两天搭建个人博客

    传送门:https://github.com/1417766861/django-blog(可直接运行,上面有步骤) 效果: 首页: 侧栏: 详情页面: 修改头像,资料,文章发布: 支持添加标签拖拽 ...

随机推荐

  1. 面试之路(16)-归并排序详解(MergeSort)递归和非递归实现

    归并排序的概念及定义 归并排序(Merge)是将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列分为若干个子序列,每个子序列是有序的.然后再把有序子序列合并为整体有序序列. 归并排序是建立 ...

  2. Graph Cuts学习笔记2014.5.16----1

    进行了一段时间的论文学习后,现在下载了一些代码,准备从OpenCV跟matlab两个方面着手搭建自己的图像分割平台,计划耗时一个月左右的时间! 昨天去西工大,听了一场Graph Asia的报告,里面有 ...

  3. C语言中如何写一个简单可移植而又足够随机的随机数生成器

    在C语言中标准库中的随机数产生函数的返回可能不是最优的,因为有些随机数生成器的低位并不随机,而另一些返回随机数的函数实现上又太复杂鸟.所以rand()%N并不是一个好方法,牛人给出的建议是使用: ra ...

  4. 演进之美,越来越美:三分钟看尽 iOS 1 ~ iOS 8 的进化史

    演进之美,越来越美:三分钟看尽 iOS 1 ~ iOS 8 的进化史 原文出处: 少数派 9 月 18 日苹果就将推出 iOS 8 正式版了,从 2007 年发布第一代 iPhone 时搭载在 iPh ...

  5. java 多线程例子

    java 多线程例子   编写具有多线程能力的程序经常会用到的方法有: run(), start(), wait(), notify(), notifyAll(), sleep(), yield(), ...

  6. 6.3 Query 语句对系统性能的影响

    我们重点分析实现同样功能的不同SQL 语句在性能方面会产生较大的差异的根本原因,并通过一个较为典型的示例来对我们的分析做出相应的验证. 为什么返回完全相同结果集的不同SQL 语句,在执行性能方面存在差 ...

  7. 使用JConsole以及VisualVM进行jvm程序的监控,排错,调优

    这里只是做一个备份,便于以后继续. 添加两个感觉好的链接吧: http://www.linuxidc.com/Linux/2015-02/113420.htm http://blog.csdn.net ...

  8. C语言实现输出一组数字中的所有奇数

    /*第二题*/ #include<stdio.h> //输入186732468 //输出173 //输入12345677 //输出13577 main(){ ;//输入的数字,数字的长度 ...

  9. C入门语言基础一[可移植性、涉及的三种文件、编程7个步骤、编译器、链接器]

    Review Questions What dose portability mean in the context of programming? 文中讲到的可移植性是什么意思?   C本身是不涉及 ...

  10. JSON-RPC远程调用协议

    1. JSON-RPC简介 2. 请求 3. 响应 4. 错误 4.1. 错误对象 4.2. 错误码 5. 批量调用 6. 示例 6.1. 列表形式参数 6.2. key-value形式参数 6.3. ...