[个人网站搭建]·Django增加评论功能

个人主页--> https://xiaosongshine.github.io/

个人网站搭建github地址:https://github.com/xiaosongshine/djangoWebs

安装django插件

pip install django-contrib-comments

配置settings.py

INSTALLED_APP=(
#...,
'django_comments',
'django.contrib.sites',
)
SITE_ID = 1

在INSTALLED_APP添加django_comments和django.contrib.sites两个应用。

在外部添加 SITE_ID=1。

django的评论库是一个站点,所以需要添加sites的应用并设置当前django工程的站点id=1

更新数据库

python manage.py migrate

配置urls.py

在 urlpatterns 中添加

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

修改前端页面显示评论列表和评论提交表单

接着,修改前端页面显示评论列表和评论提交表单。这些需要使用django_comments的模版标签,在使用标签之前导入加载:

{# 导入评论库模块的模版标签 #}
{% load comments %}

评论列表可以通过django_comments的get_comment_list模版标签获取,如下代码:

<div class="panel panel-default">
<div class="panel-heading">
<h4>评论列表</h4>
</div> <div class="panel-body">
{% get_comment_list for blog as comments %}
{% for comment in comments %}
<div class="blog_comment" name="F{{comment.id}}">
<p class="comment_title">
#{{ comment.submit_date|date:"Y-m-d H:i"}} @ {{ comment.user_name }}:
</p>
<p class="comment_content">{{ comment.comment }}</p>
</div>
{% empty %}
<span>暂无评论</span>
{% endfor %}
</div>
</div>

get_comment_list模版标签的用法是for一个模版对象,as是重命名。变量得到的评论加载即可。

而评论提交表单,最主要的是提交的url和表单字段。同样也可以通过django_comments的模版标签处理,如下代码:

<h4>新的评论</h4>
{% get_comment_form for blog as blog_form %} <form id="comment_form"
class="form-horizontal"
action="{% comment_form_target %}"
method="post"
>
{% csrf_token %} {# 必须的字段 #}
{{ blog_form.object_pk }}
{{ blog_form.content_type }}
{{ blog_form.timestamp }}
{{ blog_form.site }}
{{ blog_form.submit_date }}
{{ blog_form.security_hash }} {# 用户名字段,这个后面会修改为登录用户评论,无需填这个 #}
<div class="control-group">
<label class="control-label" for="id_name">名称: </label>
<div class="controls">
<input type="text"
id="id_name" class="input-xlarge" name="name"
placeholder="请输入您的用户名"
value="{{ user.username }}" />
</div>
</div> {# 邮箱地址字段 #}
<div class="control-group">
<label class="control-label" for="id_email">邮箱: </label>
<div class="controls">
<input type="email"
id="id_email" class="input-xlarge" name="email"
placeholder="请输入您的邮箱地址"
value="{{ user.email }}" />
</div>
</div> {# 评论内容 #}
<a name="newcomment" id="newcomment"></a>
<div class="control-group">
<label class="control-label" for="id_comment">评论: </label>
<div class="controls">
<textarea rows="6"
id="id_comment" class="input-xlarge comment" name="comment"
placeholder="请输入评论内容">
</textarea>
</div>
</div> {# 防垃圾评论 #}
<p style="display:none;">
<label for="id_honeypot">如果你在该字段中输入任何内容,你的评论就会被视为垃圾评论。</label>
<input type="text" name="honeypot" id="id_honeypot">
</p> {# 表单按钮 #}
<div class="controls">
<div class="form-actions">
<input class="btn btn-info" id="submit_btn" type="submit" name="submit" value="提交"/>
<input type="hidden" name="next" value="{%url 'detailblog' blog.id%}"/>
</div>
</div>
</form>

这一步需要注意的有两点

1.{% get_comment_form for blog as blog_form %} {% get_comment_list for blog as comments %}中blog就是你的文章内容,我的主页用的是show我就改为了:

{% get_comment_form for show as blog_form %} {% get_comment_list for show as comments %}

2.<input type="hidden" name="next" value="{%url 'detailblog' blog.id%}"/>其中的value="{%url 'detailblog' blog.id%}就是你要刷新的网页url,我的修改为了:

<input type="hidden" name="next" value="/details-{{show.id}}.html"/>

还有一个小技巧:可以通过{{ comments|length}}获取评论总数目,便于统计显示,我的实现:

<li><a href="#" class="icon fa-comment">{{ comments|length}}</a></li>​​​​​​​

重启Uwsgi和Nginx

修改Django文件和其它配置文件之后,一定要重启Uwsgi和Nginx,不然不生效。

Uwsgi和Nginx重启方法:

#查看Uwsgi进程
ps -ef|grep uwsgi 
#用kill方法把uwsgi进程杀死,然后启动uwsgi
killall -9 uwsgi
#启动方法
uwsgi -x mysite.xml #Nginx平滑重启方法
/usr/local/nginx/sbin/nginx -s reload

效果展示

Please Enjoy Yourself

欢迎大家访问我的主页尝试一下,觉得有用的话,麻烦小小鼓励一下 ><

个人网站搭建github地址:https://github.com/xiaosongshine/djangoWebs 欢迎访问

参考:http://yshblog.com/blog/5

[个人网站搭建]·Django增加评论功能(Python3)的更多相关文章

  1. Django——实现评论功能(包括评论回复)

    提示:(1)功能不全面,仅仅实现评论(2)样式简单 1.项目目录结构 2.模型 from django.db import models from django.contrib.auth.models ...

  2. 一步步搭建自己的博客 .NET版(2、评论功能)

    前言 这次开发的博客主要功能或特点:    第一:可以兼容各终端,特别是手机端.    第二:到时会用到大量html5,炫啊.    第三:导入博客园的精华文章,并做分类.(不要封我)    第四:做 ...

  3. Django之博客系统:增加评论

    3既然是博客,那肯定就有留言评论系统.在这一章就来建立一个评论系统. 1 创建一个模型来保存评论 2 创建一个表单来提交评论并且验证输入的数据 3 添加一个视图函数来处理表单和保存新的评论到数据库 4 ...

  4. BBS项目分布搭建四(点赞点踩及评论功能准备)

    BBS项目分布搭建四(点赞点踩及评论功能) 1. 点赞点踩样式准备 # 在base.html文件中 head标签内 添加css模块: {% block css %} {% endblock %} # ...

  5. Django使用forms来实现评论功能

    貌似Django从版本1.6开始就放弃了对自带的comments的使用,具体原因未查,但是现在使用Django的内部的模块也可以实现评论功能,那就是借助于forms模块,下面是我的一个小例子. 环境准 ...

  6. 如何给oneindex网盘增加评论、密码查看、read me,头提示功能。

    来自我的博客:www.resource143.com 微信公众号:资源库resource 视频教程地址 点击查看 评论功能 特性 使用 GitHub 登录 支持多语言 [en, zh-CN, zh-T ...

  7. python3搭建Django项目

    1.本次安装的python3.7版本,可前往官网下载,这里的安装不作多余介绍 2.安装虚拟环境 第一种:virtualenv:用于创建虚拟环境,实现项目之间的环境隔离,解决项目中存在的版本冲突问题 w ...

  8. Django——实现最基础的评论功能(只有一级评论)

    我对评论功能的理解: --------(1)数据库建一个评论的表 --------(2)前端建一个提交评论的form表单 --------(3)表单提交评论内容后写入到数据库评论表中 -------- ...

  9. Django自带评论功能的基本使用

    1. 模块安装 pip install django-contrib-comments 2. 注册APP INSTALLED_APP=( #..., 'django_comments', 'djang ...

随机推荐

  1. BZOJ_2594_[Wc2006]水管局长数据加强版_LCT

    BZOJ_2594_[Wc2006]水管局长数据加强版_LCT Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供 ...

  2. BZOJ_1485_[HNOI2009]有趣的数列_卡特兰数

    BZOJ_1485_[HNOI2009]有趣的数列_卡特兰数 Description 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ ...

  3. webpack下css/js/html引用图片的正确方式

    在webpack的处理下,为了使引用的图片被打包编译(以把src的图片文件编译到dist中或者对src的文件进行base64编码),应使用如下引用方式: 1. 在html/ejs等中引用图片: 借助r ...

  4. java代码之美(12)---CollectionUtils工具类

    java代码之美(12)---CollectionUtils工具类 这篇讲的CollectionUtils工具类是在apache下的, 而不是springframework下的CollectionUt ...

  5. 基于.NET的APP开发和Windows开发,异步回调差别

    在Smobiler的开发中,控件或组件及客户端功能都是通过事件或委托来进行处理的. Smobiler是基于异步非阻塞的方式来运行的 下面我们分别对Windows的和Smobiler的MessageBo ...

  6. String字符串类总结

    object类 int hashCode() Object定义的hashCode方法能为不同对象返回不同的整数.实际上是把JVM给对象分配的地址转化为整数,确保了逻辑上的唯一性.而转化的散列算法,可能 ...

  7. Java集合学习总结

    java集合 collection public interface Collection<E> extends Iterable<E> List public interfa ...

  8. jQuery --- 第四期 (jQuery动效)

    学习笔记 1.jQuery动画的淡入淡出 <!doctype html> <html> <head> <meta charset="utf-8&qu ...

  9. 网页设计(CSS&JS)

    实验一  简单静态网页设计 一. 实验目的: 复习使用记事本编辑网页的方法. 熟悉不同表单控件类型的应用. 练习使用记事本在网页中添加表单与表单元素. 二. 实验内容: 根据提供的素材设计在线调查问卷 ...

  10. SQL - Order By如何处理NULL

    问题来了.执行SQL语句 SELECT * FROM tbl ORDER BY x, y 如果用来排序的列x.y当中有NULL值,那么它们的顺序是怎样的呢? 不同的数据库有不同的答案,目前的主流数据库 ...