[个人网站搭建]·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_2580_[Usaco2012 Jan]Video Game_AC自动机+DP

    BZOJ_2580_[Usaco2012 Jan]Video Game_AC自动机+DP Description Bessie is playing a video game! In the game ...

  2. CentOS7防火墙管理firewalld

    学习apache安装的时候需要打开80端口,由于centos 7版本以后默认使用firewalld后,网上关于iptables的设置方法已经不管用了,想着反正iptable也不太熟悉,索性直接搬官方文 ...

  3. Form表单 JSON Content-type解析

    Form表单 JSON Content-type解析 1 表单Form概述 在Form表单中,参数一般有: action 表单提交的url method 提交方式:post get name 表单的属 ...

  4. mysql 使用Navicat Lite如何打开‘查询编辑器’,使用sql语句对表进行操作!

    今天第一次使用mysql,尽然连查询编辑器都找不到,研究了半天,询问了下大牛,才搞出来,准备写下来,后面方面忘记了有查找的地方,哈哈哈~~ 如何打开"查询编辑器",使用sql语句进 ...

  5. 当需要向数据库插入空值时,sql语句的判断

    方法如下: 1.int代表整形. 2.string 代表 字符型. 3.datetime ,日期类型判断如下 if(account.date!=Datetime.MinValue) { Str1.Ap ...

  6. 死磕 java集合之ArrayBlockingQueue源码分析

    问题 (1)ArrayBlockingQueue的实现方式? (2)ArrayBlockingQueue是否需要扩容? (3)ArrayBlockingQueue有什么缺点? 简介 ArrayBloc ...

  7. requests使用“proxy”代理访问接口

    在requests中使用proxy代理访问 使用前先更新requests版本为支持socks的版本.   先pip安装对应库:  >> pip install -U requests[so ...

  8. Java8新特性之四:接口默认方法和静态方法

    在JDK1.8以前,接口(interface)没有提供任何具体的实现,在<JAVA编程思想>中是这样描述的:"interface这个关键字产生了一个完全抽象的类,它根本就没有提供 ...

  9. 实用抓包工具:whistle

    无论是前端开发还是客户端,实际开发过程中,抓包都经常被用到.工欲善其事必先利其器,一款好用的抓包工具也是必须的. 说到抓包,Windows系统上不少人都用过Fiddler,Mac或Linux上用Cha ...

  10. Multi Reflection (Pro Only)

    Case 14 - Double Injection in HTML Context with Double Quotes https://brutelogic.com.br/multi/double ...