Python之路,day22-BBS基础
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python之路,day22-BBS基础相关的知识,希望对你有一定的参考价值。
Python之路,day22-BBS基础
多级评论
from django.template import Library from django.utils.safestring import mark_safe register = Library() @register.simple_tag def truncate_upload_img(img_src): print(dir(img_src)) print(img_src.name) return img_src.name.lstrip("/uploads/") @register.simple_tag def render_paginator_btn(articles,page): current_page = articles.number if abs(current_page - page) <= 5 : ele = """<li ><a href="?page={page}">{page}</a></li>""".format(page=page) return mark_safe(ele) return ‘‘ def build_comment_tree(comment_dic,obj): """递归的去把每个评论放到合适的层级里面""" for k,v in comment_dic.items(): if obj.p_node == k: #代表找他了它父节点,把自己加到k下面 print("[%s]找到了父节点[%s]" %(obj,k)) comment_dic[k][obj] = {} else: #开始进行深度查询 print("没找到,进入下一层查找",obj) build_comment_tree(comment_dic[k], obj) def build_comment_html(comment_dic,margin_arg): """循环评论的字典, 拼接html""" comment_eles = ‘‘ for k,v in comment_dic.items(): comment_eles += ‘‘‘<div style=‘border:1px dashed black;padding:10px;margin-left:{margin}px‘>{user} ---- {date} --- {comment}</div>‘‘‘ .format(user=k.user, date=k.date.strftime(‘%Y-%m-%d %H:%M:%S‘), comment=k.comment, margin=margin_arg) if v: comment_eles += build_comment_html(comment_dic[k],margin_arg+20) return comment_eles @register.simple_tag def load_comments(article_obj): #1.先把数据库里所有的这篇文章的评论查出来,转成字典 #2.递归的循环这个字典,生成html评论元素 comment_dic = { } comment_objs = article_obj.comment_set.all().order_by(‘date‘) #列表 for obj in comment_objs: if not obj.p_node:#判断obj有没有p_node,没有的话,那它自己就是顶级评论 comment_dic[obj] = {} else: #有父亲节点 build_comment_tree(comment_dic,obj) print(comment_dic) comment_list = sorted(comment_dic.items(),key=lambda x:x[0].date) print("comment objs:",comment_list) comment_html = """""" for comment_branch in comment_list: margin_arg=0 branch_ele = """<div style=‘border:1px dashed black;padding:10px;margin-left:{margin}px‘>{user} ---- {date} --- {comment}</div>""". format(user=comment_branch[0].user, date=comment_branch[0].date.strftime(‘%Y-%m-%d %H:%M:%S‘), comment=comment_branch[0].comment, margin=margin_arg) comment_html += branch_ele #开始构建它的子级评论的元素 comment_html += build_comment_html(comment_branch[1],margin_arg+20) return mark_safe(comment_html)
webqq
from django.shortcuts import render,HttpResponse from django.views.decorators.csrf import csrf_exempt from webqq.msg_handler import MsgHandler import queue,json # Create your views here. MSG_QUEUES = {} def dashboard(request): print("user--",request.user.userproifle) return render(request,"webqq/dashboard.html") #@csrf_exempt def msg_api(request): msg_obj = MsgHandler(request, MSG_QUEUES) if request.method == "POST": msg_obj.msg_send() return HttpResponse(json.dumps({"msg_send_status": 1})) else: msg_data = msg_obj.msg_recv() return HttpResponse(json.dumps(msg_data))
1 {% extends ‘index.html‘ %} 2 3 {% block extra-head-resources %} 4 <link href="/static/css/webqq_style.css" rel="stylesheet"> 5 6 {% endblock %} 7 8 9 {% block container %} 10 <div class="row" style="margin-top: 50px;height: 100%;"> 11 {% csrf_token %} 12 <div class="col-lg-2 contact_list"> 13 14 <ul class="nav nav-tabs" role="tablist"> 15 <li role="presentation"><a href="#contacts" aria-controls="contacts" role="tab" data-toggle="tab">c</a></li> 16 <li role="presentation"><a href="#groups" aria-controls="groups" role="tab" data-toggle="tab">g</a></li> 17 18 </ul> 19 20 21 <div class="tab-content"> 22 <div role="tabpanel" class="tab-pane active" id="contacts"> 23 24 <div class="list-group"> 25 {% for contact in request.user.userproifle.friends.all %} 26 27 <a href="#" class="list-group-item" contact_id="{{ contact.id }}" contact_name="{{ contact.name }}" onclick="OpenSession(this)">{{ contact }} 28 {# <span class="badge">14</span>#} 29 </a> 30 31 {% endfor %} 32 33 </div> 34 35 </div> 36 <div role="tabpanel" class="tab-pane" id="groups">groups</div> 37 </div> 38 39 40 41 </div> 42 <div class="col-lg-8 chat_panel"> 43 44 <div class="row chat_panel_header"> 45 46 <div class="hidden" style="color: green"> 47 正在与<span contact_id="" id="chat_panel_header_text"></span>聊天 48 </div> 49 </div> 50 51 <div class="row chat_panel_body"> 52 body 53 </div> 54 55 <div class="row chat_panel_input_box"> 56 57 <textarea id="msg_input_box" class="msg_input_box"> 58 59 60 61 </textarea> 62 63 </div> 64 65 </div> 66 67 68 </div> 69 70 71 {% endblock %} 72 73 74 {% block bottom-js %} 75 76 <script> 77 78 $(document).ready(function () { 79 80 csrfmiddlewaretoken = $("input[name=‘csrfmiddlewaretoken‘]").val(); 81 82 83 $("body").delegate("textarea", "keydown",function(e){ 84 if(e.which == 13) {//Enter key down 85 //send msg button clicked 86 var msg_text = $("textarea").val(); 87 if ($.trim(msg_text).length > 0){ 88 //console.log(msg_text); 89 SendMsg(msg_text); 90 } 91 //no wait the send_msg‘s call confirm msg 92 AddSentMsgIntoBox(msg_text); 93 $("textarea").val(‘‘); 94 } 95 });//end body 96 97 98 99 100 /*setInterval(function () { 101 LoadNewMsgs(); 102 },1000)*/ 103 104 LoadNewMsgs(); 105 106 107 108 109 });//end doc ready 110 111 112 function LoadNewMsgs() { 113 114 115 116 $.get("{% url ‘get_msg‘ %}",function(callback){ 117 console.log("get_msg callback:",callback); 118 return LoadNewMsgs(); 119 });//end get 120 } 121 122 function SendMsg(msg) { 123 console.log("going tosend msg" + msg); 124 125 126 127 var msg_data = { 128 ‘csrfmiddlewaretoken‘:csrfmiddlewaretoken, 129 ‘from‘:"{{ request.user.userproifle.id }}", 130 ‘to‘: $("#chat_panel_header_text").attr("contact_id"), 131 ‘data‘:msg 132 } 133 $.post("{% url ‘msg_api‘ %}", msg_data ,function(callback){ 134 135 var callback = JSON.parse(callback); //json反序列化 136 console.log("msg_send_status:",callback.msg_send_status); 137 if ( callback.msg_send_status != 1){ 138 alert("消息发送失败:"+ msg); 139 } 140 });//end post 141 142 143 } 144 function AddSentMsgIntoBox(msg_text) { 145 var d = new Date(); 146 var msg_ele = "<div> <div>{{ request.user.userproifle.name }} "+ d.getHours() + ":" +d.getMinutes() + ":" + d.getSeconds() + "</div>"; 147 msg_ele += "<div>" + msg_text +"</div> </div>"; 148 $(".chat_panel_body").append(msg_ele); 149 150 151 $(‘.chat_panel_body‘).animate({ 152 scrollTop: $(‘.chat_panel_body‘)[0].scrollHeight}, 500) 153 154 } 155 function OpenSession(ele) { 156 var contact_id = $(ele).attr("contact_id"); 157 var contact_name = $(ele).attr("contact_name"); 158 $(ele).addClass("active"); 159 $(ele).siblings().removeClass("active"); 160 $("#chat_panel_header_text").text(contact_name); 161 $("#chat_panel_header_text").parent().removeClass("hidden"); 162 $("#chat_panel_header_text").attr("contact_id",contact_id); 163 164 } 165 166 167 168 169 170 171 </script> 172 173 174 {% endblock %}
以上是关于Python之路,day22-BBS基础的主要内容,如果未能解决你的问题,请参考以下文章