-
评论树的使用
# 评论树 def comment_tree(request,article_id): ret = list(models.Comment.objects.filter(article_id=article_id).values('content','parent_comment_id','pk')) return JsonResponse(ret,safe=False)
HTML
<div class="comment_tree" style="margin-top: 100px"> </div>
JS
$.ajax({ url:'/blog/comment_tree/'+'{{ article_obj.pk }}/', success:function (date) { $.each(date,function (index,comment_dict) { var s = '<div class = "comment_item"comment_id = '+comment_dict.pk+'> <span class="content">'+comment_dict.content+'</span> </div>'; if (comment_dict.parent_comment_id) { pid = comment_dict.parent_comment_id; $("[comment_id = "+ pid +"]").append(s); } else { $('.comment_tree').append(s); } }); } })
.CSS
.comment_item{ margin-left: 20px; }
-
点赞的实现
from django.db.models import F,Q def up_down(request): article_id = request.POST.get('article_id') is_up = json.loads(request.POST.get('is_up')) user = request.user respones = {'status':True} try: models.ArticleUpDown.objects.create(user=user,article_id = article_id,is_up = is_up) models.Article.objects.filter(pk = article_id).update(up_count = F('up_count')+1) except Exception as e: respones['status'] = False if is_up: respones['warring'] = '你已经推荐过了' else: respones['warring'] = '你已经反对过了' return JsonResponse(respones)
js代码
$('.action').on('click', function () { var $info = $('.info'); if (($info).attr('username')) { var is_up = $(this).hasClass('diggit'); var article_id = $info.attr('article_id'); var skr = function (Elen) { var val = Elen.text(); val = parseInt(val) + 1; Elen.text(val); }; $.ajax({ url: '/blog/up_down/', type: 'post', data: { is_up: is_up, article_id: article_id, 'csrfmiddlewaretoken': $("[name='csrfmiddlewaretoken']").val(), }, success: function (data) { if (data.status) { //赞,踩成功 if (is_up) { skr($('#digg_count')); } else { skr($('#buryit')); } } else { //重复提交 $('#digg_tips').html(data.warring); setTimeout(function () { $('#digg_tips').html(''); }, 1000) } } }) } else { location.href = "/login/"; } });
-
防止XSS攻击
from bs4 import BeautifulSoup
# 过滤非法标签 for tag in bs.find_all(): if tag.name in ['script','link']: tag.decompose() //去除掉
-
富文本编辑器
1、下载
kindeditor
<script charset="utf-8" src="/static/kindeditor/kindeditor-all-min.js"></script>
2、js代码
KindEditor.ready(function (k) { window.editor = k.create('#article_content',{ width:'900px', height:'500px', resizeType:0, uploadJson:'/upload/', extraFileUploadParams:{ csrfmiddlewaretoken:$('[name = "csrfmiddlewaretoken"]').val() }, filePostName:'upload_img' }); })
3、views.py
from bs4 import BeautifulSoup def add_article(request): flag = '1' handel = '添加文章' if request.method == "POST": title = request.POST.get('title') article_content = request.POST.get('article_content') user = request.user bs = BeautifulSoup(article_content,'html.parser') desc = bs.text[0:150]+'...' # 过滤非法标签 for tag in bs.find_all(): if tag.name in ['script','link']: tag.decompose() article_obj = models.Article.objects.create(user=user,title=title,desc=desc) models.ArticleDetail.objects.create(content=str(bs),article=article_obj) return HttpResponse('ok') return render(request,'add_article.html',{'flag':flag,'handel':handel})
python笔记(博客项目,评论树的实现,点赞的实现,防止XSS攻击,富文本编辑器)
最新推荐文章于 2020-12-23 09:08:17 发布