注: 此处的小评论涉及数据库操作

初级小评论代码

settings.py

INSTALLED_APPS = [
...
'app01', # 注册app
]
STATICFILES_DIRS = (os.path.join(BASE_DIR, "statics"),) # 现添加的配置,这里是元组,注意逗号
TEMPLATES = [
...
'DIRS': [os.path.join(BASE_DIR, 'templates')],
]

urls.py

from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from app01 import views
urlpatterns = [
# 评论
url('comment/', views.Node.Comment)
]

views.py

from django.shortcuts import render
from django.shortcuts import redirect
from django.shortcuts import HttpResponse
from app01 import models class Node:
@staticmethod
def digui(ret, row):
for item in ret:
if row['parent_id'] == item['id']:
row['children'] = []
item['children'].append(row)
break
else:
Node.digui(item['children'], row)
@staticmethod
def create_commen_tree(comment_list):
ret = []
'''
ret=[
{'id':1, 'content':'Python', 'user':'AAA', 'parent_id': None, 'children':[]},
{'id':2, 'content':'JAVA', 'user':'AAA', 'parent_id': None, 'children':[]},
{'id':3, 'content':'C++', 'user':'AAA', 'parent_id': None, 'children':[]}
]
'''
for row in comment_list:
if not row['parent_id']:
row['children'] = []
ret.append(row)
else:
Node.digui(ret, row)
return ret # 评论
def Comment(request):
'''
【标准】方式一:这里是从数据库内取值
news_id = 1
# 获取新闻id=1的新闻,页面不能直接循环,否则显示不出来层级关系
commen_list = models.Comment.objects.filter(news_id=news_id)
for row in commen_list:
print(row.id, row.content, row.userInfo.name, row.parent_id) return HttpResponse("OK")
'''
# 方式二: 这里是模仿来实现评论的级别关系
comment_list = [
{'id':1, 'content':'Python', 'user':'AAA', 'parent_id': None},
{'id':2, 'content':'JAVA', 'user':'AAA', 'parent_id': None},
{'id':3, 'content':'C++', 'user':'AAA', 'parent_id': None},
{'id':4, 'content':'PHP', 'user':'BBB', 'parent_id': 1},
{'id':5, 'content':'HTML', 'user':'CCC', 'parent_id': 1},
{'id':6, 'content':'CSS', 'user':'DDD', 'parent_id': 4},
{'id':7, 'content':'JavaScript', 'user':'EEE', 'parent_id': 2},
{'id':8, 'content':'Python', 'user':'FFF', 'parent_id': 3},
{'id':9, 'content':'C#', 'user':'GGG', 'parent_id': 2},
{'id':10, 'content':'Shell', 'user':'HHH', 'parent_id': 4},
{'id':11, 'content':'C语言', 'user':'XXX', 'parent_id': 6},
]
comment_tree = Node.create_commen_tree(comment_list)
for item in comment_tree:
print(item)
return HttpResponse("OK")

models.py

from django.db import models

# Create your models here.

# 新闻表
class News(models.Model):
title = models.CharField(max_length=32) # 用户表
class UserInfo(models.Model):
name = models.CharField(max_length=32) # 评论表
class Comment(models.Model):
content = models.CharField(max_length=150)
userInfo = models.ForeignKey("UserInfo", on_delete=True)
news = models.ForeignKey("News", on_delete=True)
parent = models.ForeignKey("self", on_delete=True, related_name='o', null=True)
ctime = models.DateTimeField(auto_now_add=True, null=True)

页面显示;

初始化数据库

python manage.py makemigrations

python manage.py migrate

高级小评论代码

原理解析:

因为li[列表]是引用类型,所以v和li引用同一个地址,v增加后li也同时增加

li = [
[1,2,3,4,5],
[1,2,3,4,5,6],
]
v = li
v.append([000,111,222])
print(li)
print(v)

同上原理,每个字典更改后,其内部的内容包括引用的内容都会修改

li = [
{'id': 10, 'content': 'Shell', 'user': 'HHH', 'parent_id': 4},
{'id': 11, 'content': 'C语言', 'user': 'XXX', 'parent_id': 6},
]
v = li
for item in li:
item.update({'children' : []}) # 每个列表里面添加children属性,有则更改,无责添加
print('li里的元素:\n', li) # 为每隔row添加children属性
for row in li:
if row['id'] == 11:
row['children'].append(row) li[1]['user'] = 'FTL' # 此时li[1]里面的内容全部修改了包括内部children中的name
# 在item里的children元素添加item内容
for item in li:
print('children添加元素:\n', item, item['children'])

内容显示:

小评论高级算法:

comment_list = [
{'id':1, 'content':'Python', 'user':'AAA', 'parent_id': None},
{'id':2, 'content':'JAVA', 'user':'AAA', 'parent_id': None},
{'id':3, 'content':'C++', 'user':'AAA', 'parent_id': None},
{'id':4, 'content':'PHP', 'user':'BBB', 'parent_id': 1},
{'id':5, 'content':'HTML', 'user':'CCC', 'parent_id': 1},
{'id':6, 'content':'CSS', 'user':'DDD', 'parent_id': 4},
{'id':7, 'content':'JavaScript', 'user':'EEE', 'parent_id': 2},
{'id':8, 'content':'Python', 'user':'FFF', 'parent_id': 3},
{'id':9, 'content':'C#', 'user':'GGG', 'parent_id': 2},
{'id':10, 'content':'Shell', 'user':'HHH', 'parent_id': 4},
{'id':11, 'content':'C语言', 'user':'XXX', 'parent_id': 6},
]
'''
方法一: for循环,效率低
ret = []
for it in comment_list:
it.update({'children': []})
for item in comment_list:
current_row = item
current_row_parentId = current_row['parent_id']
if not current_row_parentId:
ret.append(current_row)
else:
for row in comment_list: # 效率低,从头到尾又循环了一遍
if row['id'] == current_row_parentId:
row['children'].append(item)
print(ret)
'''
# 方法二: 利用字典的get效率更高,因为字典的key会转换为哈希字符串,get相当于根据sql里面的索引查找
ret = []
comment_list_dict={}
for item in comment_list:
item.update({"children":[]})
#有则更改,无责添加: comment_list_dict={1:{'id':1, 'content':'Python', 'user':'AAA', 'parent_id': None},}
comment_list_dict[item['id']]=item
for row in comment_list:
current_now_parentID = row['parent_id']
parent_row = comment_list_dict.get(current_now_parentID) # 找到了父级row
if not parent_row:
ret.append(row)
else:
parent_row['children'].append(row) # 直接给父级row的children添加当前元素
print(ret)

小评论高级完整版[前台处理数据,推荐使用]

settings.py

INSTALLED_APPS = [
...
'app01', # 注册app
]
STATICFILES_DIRS = (os.path.join(BASE_DIR, "statics"),) # 现添加的配置,这里是元组,注意逗号
TEMPLATES = [
...
'DIRS': [os.path.join(BASE_DIR, 'templates')],
]

urls.py

from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from app01 import views
urlpatterns = [
# 评论
url('comment/', views.comment),
url('index/', views.index),
]

views.py

from django.shortcuts import render, redirect, HttpResponse
from app01 import models #小评论高级算法
def comment(request):
'''
数据库的操作:
nid = request.GET.get('nid')
comment_list=models.obects.filter(nid=nid).values('id',...) # 利用value进行字段内容的获取
commont_list --> ret [进行树形结构的转换,这里直接用固定的内容演示]
'''
comment_list = [
{'id':1, 'content':'Python', 'user':'AAA', 'parent_id': None},
{'id':2, 'content':'JAVA', 'user':'AAA', 'parent_id': None},
{'id':3, 'content':'C++', 'user':'AAA', 'parent_id': None},
{'id':4, 'content':'PHP', 'user':'BBB', 'parent_id': 1},
{'id':5, 'content':'HTML', 'user':'CCC', 'parent_id': 1},
{'id':6, 'content':'CSS', 'user':'DDD', 'parent_id': 4},
{'id':7, 'content':'JavaScript', 'user':'EEE', 'parent_id': 2},
{'id':8, 'content':'Python', 'user':'FFF', 'parent_id': 3},
{'id':9, 'content':'C#', 'user':'GGG', 'parent_id': 2},
{'id':10, 'content':'Shell', 'user':'HHH', 'parent_id': 4},
{'id':11, 'content':'C语言', 'user':'XXX', 'parent_id': 6},
]
ret = []
comment_list_dict={}
for item in comment_list:
item.update({"children":[]})
# 有则更改,无责添加: comment_list_dict={1:{'id':1, 'content':'Python', 'user':'AAA', 'parent_id': None},}
comment_list_dict[item['id']]=item for row in comment_list:
current_now_parentID = row['parent_id']
parent_row = comment_list_dict.get(current_now_parentID) # 找到了父级row
if not parent_row:
ret.append(row)
else:
parent_row['children'].append(row) # 直接给父级row的children添加当前元素
print(ret)
import json
return HttpResponse(json.dumps(comment_list)) def index(request):
return render(request, 'index.html')

models.py[未用到,静态数据源]

from django.db import models
# Create your models here.
# 新闻表
class News(models.Model):
title = models.CharField(max_length=32)
# 用户表
class UserInfo(models.Model):
name = models.CharField(max_length=32)
# 评论表
class Comment(models.Model):
content = models.CharField(max_length=150)
userInfo = models.ForeignKey("UserInfo", on_delete=True)
news = models.ForeignKey("News", on_delete=True)
parent = models.ForeignKey("self", on_delete=True, related_name='o', null=True)
ctime = models.DateTimeField(auto_now_add=True, null=True)

templates/comment.html

<!DOCTYPE html>
<html lang="en">
<head> <meta charset="UTF-8"> </head>
<body> </body>
</html>

templates/index.html

<!DOCTYPE html>
<html lang="en">
<head> <meta charset="UTF-8">
<style>
.comment-box {
margin-left: 10px;
}
</style>
</head>
<body>
<div class="item">
<h2>[静态演示效果]</h2>
<a nid="17" class="com">评论</a>
{#comment-box表示一个评论#}
<div class="comment-list">
<div class="comment-box">
<span>Python开发</span>
<div class="comment-box">
<span>Java开发</span>
<div class="comment-box">
<span>C开发</span>
</div>
</div>
<div class="comment-box">
<span>PHP开发</span>
</div>
</div>
<div class="comment-box">
<span>手机厂商</span>
<div class="comment-box">
<span>Huawei</span>
<div class="comment-box">
<span>XiaoMi</span>
</div>
</div>
<div class="comment-box">
<span>Apple</span>
</div>
</div>
</div>
</div><hr>
<div class="item">
<h3>动态演示效果,点击显示评论</h3>
<a nid="18" class="com">评论</a>
</div><hr>
</body>
<script src="/static/jquery-2.1.4.min.js"></script>
<script>
// 页面加载完成后实现绑定小姑
$(function () {
bindCommentEvent();
}); function bindCommentEvent() {
$(".com").click(function () {
var news_id = $(this).attr('nid');
console.log(news_id);
var $this = $(this);
$.ajax({
url:'/comment/',
type:'GET',
// 一个date引发的错误
data: {nid:news_id},
dataType: "JSON",
success: function (args) {
console.log(args);
create_tree(args, $this);
}
})
})
}
// 递归
function digui(child_data) {
var html = "";
$.each(child_data, function (k1, v1) {
var b = '<div class="comment-box"><span>';
b += v1.content;
b += '</span>';
son = digui(v1.children);
b += son;
b += '</div>';
html += b;
});
console.log("HTML:",html)
return html;
}
function create_tree(data,$this) {
var html = '<div class="comment-list">';
{# 注意这里是在函数内获取到key,value,然后从value里面获取内容#}
$.each(data, function (k, v) {
var a = '<div class="comment-box"><span>';
a += v.content + "</span>";
// 添加子评论
var child = digui(v.children);
console.log(child);
a += child;
a +='</div>';
html += a;
});
html += '</div>';
console.log($this);
$this.append(html);
}
</script>
</html>

页面显示;

初始化数据库

python manage.py makemigrations
python manage.py migrate

小评论最终版[后台进行tempaltetags来处理递归数据,不推荐]

settings.py

INSTALLED_APPS = [
...
'app01', # 注册app
]
STATICFILES_DIRS = (os.path.join(BASE_DIR, "statics"),) # 现添加的配置,这里是元组,注意逗号
TEMPLATES = [
...
'DIRS': [os.path.join(BASE_DIR, 'templates')],
]

urls.py

from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from app01 import views
urlpatterns = [
# 评论
url('comment/', views.comment),
url('index/', views.index),
]

views.py

from django.shortcuts import render, redirect, HttpResponse
from app01 import models #小评论高级算法
def comment(request):
'''
数据库的操作:
nid = request.GET.get('nid')
comment_list=models.obects.filter(nid=nid).values('id',...) # 利用value进行字段内容的获取
commont_list --> ret [进行树形结构的转换,这里直接用固定的内容演示]
'''
comment_list = [
{'id':1, 'content':'Python', 'user':'AAA', 'parent_id': None},
{'id':2, 'content':'JAVA', 'user':'AAA', 'parent_id': None},
{'id':3, 'content':'C++', 'user':'AAA', 'parent_id': None},
{'id':4, 'content':'PHP', 'user':'BBB', 'parent_id': 1},
{'id':5, 'content':'HTML', 'user':'CCC', 'parent_id': 1},
{'id':6, 'content':'CSS', 'user':'DDD', 'parent_id': 4},
{'id':7, 'content':'JavaScript', 'user':'EEE', 'parent_id': 2},
{'id':8, 'content':'Python', 'user':'FFF', 'parent_id': 3},
{'id':9, 'content':'C#', 'user':'GGG', 'parent_id': 2},
{'id':10, 'content':'Shell', 'user':'HHH', 'parent_id': 4},
{'id':11, 'content':'C语言', 'user':'XXX', 'parent_id': 6},
]
ret = []
comment_list_dict={}
for item in comment_list:
item.update({"children":[]})
# 有则更改,无责添加: comment_list_dict={1:{'id':1, 'content':'Python', 'user':'AAA', 'parent_id': None},}
comment_list_dict[item['id']]=item for row in comment_list:
current_now_parentID = row['parent_id']
parent_row = comment_list_dict.get(current_now_parentID) # 找到了父级row
if not parent_row:
ret.append(row)
else:
parent_row['children'].append(row) # 直接给父级row的children添加当前元素
print(ret)
return render(request,'comment.html', {"ret":ret}) def index(request):
return render(request, 'index.html')

models.py[未用到,静态数据源]

from django.db import models
# Create your models here.
# 新闻表
class News(models.Model):
title = models.CharField(max_length=32)
# 用户表
class UserInfo(models.Model):
name = models.CharField(max_length=32)
# 评论表
class Comment(models.Model):
content = models.CharField(max_length=150)
userInfo = models.ForeignKey("UserInfo", on_delete=True)
news = models.ForeignKey("News", on_delete=True)
parent = models.ForeignKey("self", on_delete=True, related_name='o', null=True)
ctime = models.DateTimeField(auto_now_add=True, null=True)

App01/tempalatetags/hhh.py

from django import template
from django.utils.safestring import mark_safe
register = template.Library() def diGui(children_list):
html = ""
for cv in children_list:
b = '<div class="comment-box"><span>'
b += cv['content'] + "</span>"
b += diGui(cv['children'])
b += "</div>"
html += b
return html @register.simple_tag
def create_tree(comment_list):
html = '<div class="comment-list">'
for v in comment_list:
a = '<div class="comment-box"><span>'
a += v['content'] + "</span>"
a += diGui(v['children'])
a += "</div>"
html += a
return mark_safe(html) # 不添加界面只显示源代码,并不进行html渲染

templates/comment.html

{% load hhh %}
{% create_tree ret %}

templates/index.html

<!DOCTYPE html>
<html lang="en">
<head> <meta charset="UTF-8">
<style>
.comment-box {
margin-left: 10px;
}
</style>
</head>
<body>
<div class="item">
<h2>[静态演示效果]</h2>
<a nid="17" class="com">评论</a>
{#comment-box表示一个评论#}
<div class="comment-list">
<div class="comment-box">
<span>Python开发</span>
<div class="comment-box">
<span>Java开发</span>
<div class="comment-box">
<span>C开发</span>
</div>
</div>
<div class="comment-box">
<span>PHP开发</span>
</div>
</div>
<div class="comment-box">
<span>手机厂商</span>
<div class="comment-box">
<span>Huawei</span>
<div class="comment-box">
<span>XiaoMi</span>
</div>
</div>
<div class="comment-box">
<span>Apple</span>
</div>
</div>
</div>
</div><hr>
<div class="item">
<h3>动态演示效果,点击显示评论</h3>
<a nid="18" class="com">评论</a>
</div><hr>
</body>
<script src="/static/jquery-2.1.4.min.js"></script>
<script>
// 页面加载完成后实现绑定小姑
$(function () {
bindCommentEvent();
}); function bindCommentEvent() {
$(".com").click(function () {
var news_id = $(this).attr('nid');
console.log(news_id);
var $this = $(this);
$.ajax({
url:'/comment/',
type:'GET',
// 一个date引发的错误
data: {nid:news_id},
dataType: "html",
success: function (args) {
console.log('HHH');
console.log(args);
{# create_tree(args, $this);#}
$this.after(args);
}
})
})
}
/**
function digui(child_data) {
var html = "";
$.each(child_data, function (k1, v1) {
var b = '<div class="comment-box"><span>';
b += v1.content;
b += '</span>';
son = digui(v1.children);
b += son;
b += '</div>';
html += b;
});
console.log("HTML:",html)
return html;
} function create_tree(data,$this) {
var html = '<div class="comment-list">';
{# 注意这里是在函数内获取到key,value,然后从value里面获取内容#}
$.each(data, function (k, v) {
var a = '<div class="comment-box"><span>';
a += v.content + "</span>";
// 添加子评论
var child = digui(v.children);
console.log(child);
a += child;
a +='</div>';
html += a;
});
html += '</div>';
console.log($this);
$this.after(html);
}
**/
</script>
</html>

页面显示;

初始化数据库

python manage.py makemigrations
python manage.py migrate

Python学习---抽屉框架分析[小评论分析]0315的更多相关文章

  1. Python学习---抽屉框架分析[ORM操作]180314

    Django ORM操作     1. 字段操作         class User(model.Model);             u=字段        用处:            1 . ...

  2. Python学习---抽屉框架分析[数据库设计分析]180313

    基本的: models.py ####################################以下都是抽屉的代码#################################### fro ...

  3. Python学习---抽屉框架分析[点赞功能/文件上传分析]0317

    点赞功能分析 前台传递过来新闻id[new_id]和session[session内有用户ID和用户之间的信息]到后台 后台News数据库内用户和新闻是多对多的关系,查看第三张表中的内容,判读用户Id ...

  4. Python学习---抽屉框架分析[点赞功能分析]

    实际上就是多了一个隐藏的span标签,内容是+1,配合setInterval实现的动态效果 settings.py INSTALLED_APPS = [ ... 'app01', # 注册app ] ...

  5. [TimLinux] Python学习内容框架

    以下内容主体来自<Python学习手册第四版>,大致整理出的方向 1. 第一部分:使用入门 介绍Python语法之前,先对Python的的各个方面进行一个比较宽广的介绍,包含对Python ...

  6. 转载-python学习笔记之常用模块用法分析

    内置模块(不用import就可以直接使用)   常用内置函数   help(obj) 在线帮助, obj可是任何类型 callable(obj) 查看一个obj是不是可以像函数一样调用 repr(ob ...

  7. python学习:猜数字小游戏

    在学习python过程中,没有项目做,就想到哪儿弄到哪儿. 头一发.让机器随机固定一个数字,然后让人去猜. 就这么简单.代码如下: #-*- encoding:utf8 -*- import rand ...

  8. Python 学习(1) 简单的小爬虫

    最近抽空学了两天的Python,基础知识都看完了,正好想申请个联通日租卡,就花了2小时写了个小爬虫,爬一下联通日租卡的申请页面,看有没有好记一点的手机号~   人工挑眼都挑花了. 用的IDE是PyCh ...

  9. Python学习——web框架

    对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. WSGI(Web Server Gateway Interface)是一种规范,它定义了使用p ...

随机推荐

  1. Eclipse/MyEclipse按任何键,都可以提示?(最强帮手)

    说明: 一般在Eclipse ,MyEclipse代码里面,打个foreach,switch等这些,是无法得到代码提示的(不信自己试试),其他的就更不用说了,而在Microsoft Visual St ...

  2. C#删除只读文件

    File.SetAttributes(fileRealPath, FileAttributes.Normal);//先将文件设置成普通属性 //...你的删除文件的代码

  3. C#判断字符串中是否包含一个子字符串是可以直接使用Contains()方法

    1. 以前判断一个字符串中是否包含另一个子字符串时,习惯使用 IndexOf(); string str = "ABC@QQ"; if(str.IndexOf("@&qu ...

  4. leetcode简单题目两道(2)

    Problem Given an integer, write a function to determine if it is a power of three. Follow up: Could ...

  5. CSS position属性absolute relative等五个值的解释

    DIV CSS position绝对定位absolute relative教程篇 常常使用position用于层的绝对定位,比如我们让一个层位于一个层内具体什么位置,为即可使用position:abs ...

  6. 阿里云 ECS服务器安装流程

    1. 挂载硬盘 2. 上传文件 3 .vncserver unset DISPLAY who -u export DISPLAY= xhost + 4. java /etc/profile expor ...

  7. 经典实用的iptables shell脚本

    先解释一下iptables里的参数意思:A: 添加 (跟链)-I: 插入-p: 跟协议-s: 源IP-d: 目标IP-j: 操作行为-t: 加表--to-source:SNAT用,表示改成的SNAT源 ...

  8. angular1的 伪MVC

    以下的代码是自己对angular1的一些理解.如果非要按照mvc的这种模式开发..可以用以下的这种方式理解. //userFactorys.js 这是服务方法 return 的方法取得json数据里的 ...

  9. DataGridView 获取当前单元格

    获取DataGridview控件中的当前单元格,是通过DataGridview的Rows属性和Column属性的索引来取得的,他们的索引都是从0开始的. Private void datagridvi ...

  10. 十四、ReentrantLock重入锁

    一.简介 JDK提供了Lock接口来实现更丰富的锁控制,ReentrantLock即Lock接口的实现 JDK文档:http://tool.oschina.net/uploads/apidocs/jd ...