kindeditor文字编辑器 文章点赞评论操作
1、kindeditor文字编辑器
settings.py
# media配置 MEDIA_ROOT=os.path.join(BASE_DIR,"blog","media") # # blog 是app名字,会放在blog_amedia_avatar文件夹中 MEDIA_URL="/media/"
html
内容:<br> <textarea name="article_con" id="article_box" cols="30" rows="10"></textarea> {% csrf_token %} <script src="/static/kindeditor/kindeditor-all.js"></script> <script> {#http://kindeditor.net/docs/option.html#filepostname#} KindEditor.ready(function (k) { window.editor=k.create("#article_box",{ width:600, height:400, {#2或1或0,2时可以拖动改变宽度和高度,1时只能改变高度,0时不能拖动。#} resizeType:0, {#指定上传文件的服务器端程序。#} uploadJson:"/upload_img/", {#上传图片、Flash、视音频、文件时,支持添加别的参数一并传到服务器。#} extraFileUploadParams:{"csrfmiddlewaretoken":$("[name='csrfmiddlewaretoken']").val()}, {#指定上传文件form名称。#} filePostName:"img" }) }) </script>
views.py( 创建 blog_media_article_imgs文件夹 上传的图片会放在这里面)
from bbs_blog import settings import os def upload_img(request): print(request.POST) print(request.FILES) img_obj=request.FILES.get("img") import os media_path=settings.MEDIA_ROOT path=os.path.join(media_path,"article_imgs",img_obj.name) f=open(path,"wb") for line in img_obj: f.write(line) f.close() res={ "url":"/media/article_imgs/"+img_obj.name, "error":0, } import json return HttpResponse(json.dumps(res))
2、文章点赞评论操作
html
{#点赞或者踩页面#} <div class="clearfix"> <div id="div_digg"> <div class="diggit digg"> <p>点赞<span class="diggnum" id="digg_count">({{ article.up_count }})</span></p> </div> <hr> <div class="buryit digg"> <p>踩<span class="burynum" id="bury_count">({{ article.down_count }})</span></p> </div> <div> <span class="error"></span> </div> {% csrf_token %} </div> </div> <div id="digg_word" class="pull-right"></div>
<script> {#点赞或者踩灭js#} $("#div_digg .digg").on("click", function () { {#用户必须登录才能点赞或者踩#} if ("{{ request.user.username }}") { var is_up = $(this).hasClass("diggit"); console.log(is_up); var article_id = "{{ article.pk}}"; {#下面代码有问题#} {#var article_id=$(".article_pk").val();#} {#var article_id=parseInt($(".article_pk").val());#} console.log(article_id); console.log(typeof article_id) $.ajax({ url: "/blog/poll/", type: "post", data: { "is_up": is_up, "article_id": article_id, csrfmiddlewaretoken: $("[name='csrfmiddlewaretoken']").val(), }, success: function (data) { console.log(data); if (data.state) { {#第一次点赞或者踩成功#} if (is_up) { {#var up_count=parseInt( $("#digg_count").text() )+1#} up_number = data.msg $("#digg_count").text(up_number) } else { {#var down_count=parseInt( $("#bury_count").text() )+1#} down_number = data.msg $("#bury_count").text(down_number) } } {#重复操作点赞或者踩#} else { if (data.msg) {#以前是点赞#} { $(".error").text("您已经推荐过").css({"color": "red"}) } else { $(".error").text("您已经踩过").css({"color": "red"}) } } } }) } else { location.href = "/login/" } }) </script>
models.py( is_up = models.BooleanField(default=True) )
class ArticleUpDown(models.Model): """ 点赞表 """ nid = models.AutoField(primary_key=True) user = models.ForeignKey('UserInfo', null=True) article = models.ForeignKey("Article", null=True) is_up = models.BooleanField(default=True) class Meta: unique_together = [ ('article', 'user'), ]
views.py
import json from django.db.models import F from django.db import transaction # 数据库事务 def poll(request): if request.is_ajax(): article_id = request.POST.get("article_id") print(article_id) user_id=request.user.pk print(user_id) is_up = request.POST.get("is_up") print(is_up) print(type(is_up)) # is_up 是个str类型 ,models.py中只可以存bool值,所以应该转为bool is_up=json.loads(is_up) res = {"state": None, "msg": None} # 如果user在页面两次点击赞或者踩,会报错,此时使用try try: with transaction.atomic(): ArticleUpDown.objects.create(user_id=user_id, article_id=article_id, is_up=is_up) if is_up: # is_up 已经变成bool值。 Article.objects.filter(pk=article_id).update(up_count=F("up_count") + 1) up_number = Article.objects.filter(pk=article_id).first().up_count print("up_number", up_number) res = {"state": True, "msg": up_number} else: Article.objects.filter(pk=article_id).update(down_count=F("down_count") + 1) down_number = Article.objects.filter(pk=article_id).first().down_count print("down_number", down_number) res = {"state": True, "msg": down_number} except Exception as e: res={"state":False, "msg": None} res["msg"]=ArticleUpDown.objects.filter(user_id=user_id, article_id=article_id).first().is_up from django.http import JsonResponse return JsonResponse(res)
res={"state":False, "msg": None}
# state 表示是否操作过,如果操作过,不管点赞还是踩灭都是True;
# msg 表示返回的内容:up_number/down_number/第一次操作是点赞还是踩灭