django自制後臺左側導航程式碼
阿新 • • 發佈:2018-11-28
自定義一個sort.py頁面:
寫入一下程式碼:
class lanmusort(object):
def __init__(self):
self.arr=[]
def lanmuget(self,obj):
for value in obj:
if value['pid']==0:
value['lists']=[]
self.arr.append(value)
self.lanmuget_two(obj,value['id'],value['lists'])
del obj
return self.arr
def lanmuget_two(self,obj,pid,lists):
for value in obj:
if value['pid'] == pid:
value['lists'] = []
lists.append(value)
self.lanmuget_two(obj,value['id'],value['lists'])
def __del__(self):
del self.arr
資料庫查詢頁面
view.py:(引入上面的自定義頁面)
# 輸出左側欄目列表
left_lanmu = models.AdminRule.objects.order_by('sort').all().values("id","title","name","sort","is_top","pagenumber","pid",)
re_left_lanmu = lanmu.lanmusort()
relanmu = re_left_lanmu.lanmuget(left_lanmu)