S20_DAY21--课堂笔记

今日重点

点赞与踩灭
评论
beautifulsoup4
kindeditor工具条的使用--如何上传文件

part1:点赞与踩灭

1.

reder(index.html)

静态文件js不能render,视图views中的变量,不能在reder渲染的时候,传递给静态js。

解决办法:新加标签,把需要传递的值作为新标签的属性。


2.

ajax提交POST,data:{csrftokenxxx:$(name="csrftokenxxx").val()}
3.

js中的is_up的布尔值与python中的布尔值通过json交互

4.

两个标签(点赞+踩灭)用同一个js事件同一个class。设置相同的class,$(sameClass).hasclass(upup/downdown)

5.

点赞表:点赞用户user与文章id 联合唯一
即只允许点赞 或 踩灭 一次

try:
插入数据
except Exception as e:
state=Flase

 

6.

与ajax交互的视图views:
def digg:

return JsonResponse(res)

7.

点赞前请登录:
$(#div.digg).after(s)

8.

原子性操作:
from django.db import transaction

 

9.

后端数据交互要用后端的值,不要用前端的。
js取到的is_up没有数据库里的is_up可靠

10.

$(#button).click(function{
if $(this)[0]==$(.up)[0]:True
elif $(this)[0]==$(.down)[0]:False
})

此处非老师讲解思路,而是区分比较this对象,来判断是点赞还是踩灭。

注意:this绑定是click的那个对象 ,用js的方式比较,而不是jquery。

part2:评论

1.提交根评论
2.显示根评论{
--render
--ajax
}
3.提交子评论
4.显示子评论{
--reder
--ajax}
5.显示评论树

提交评论时,为了时区能正确显示Asia/Shanghai,需做如下改动

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'   --->'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = True----> False
settings.py

 

part3:beautifulsoup4

beautifulsoup4应用:
1.摘要(博客文章存到数据库里是带标签的,摘要截取的是文字,不要把标签截进去)
2.删除script标签(保证网站安全)

def add_article(request):
    if request.method=="POST":
        title=request.POST.get("title")
        article_content=request.POST.get("article_content")

        from bs4 import BeautifulSoup

        bs=BeautifulSoup(article_content,"html.parser")
        #  过滤content


        for tag in bs.find_all():

            if tag.name=="script":
                print(tag.name)
                tag.decompose()

        print("=====>",bs.prettify())

        article_content=bs.prettify()
        desc=bs.text[0:150]
        obj=Article.objects.create(user=request.user,title=title,desc=desc)
        ArticleDetail.objects.create(article=obj,content=article_content)
        print("article_content",article_content)

        return HttpResponse("OK")

    return render(request,"add_article.html")

 拓展阅读:爬虫基础库

part4:kindeditor工具条

下载地址:http://kindeditor.net/down.php 

猜你喜欢

转载自www.cnblogs.com/shangdelu/p/9121065.html