[个人网站搭建]·Django增加评论功能(Python3)
[個人網站搭建]·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
轉載于:https://www.cnblogs.com/xiaosongshine/p/10615575.html
總結
以上是生活随笔為你收集整理的[个人网站搭建]·Django增加评论功能(Python3)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 词典对象 NSDictionary与NS
- 下一篇: 基于TCP和多线程实现无线鼠标键盘-Ge