75

Original link: http://www.cnblogs.com/Doner/p/11044194.html

day75

Today's content:

Priv1 secondary menu button

信息管理
    客户列表
    学生列表
    
财务管理
    缴费列表
    ...


menu
    id title icon       
    
权限表
    id  title url  is_menu  foreignkey(menu_id) 
    1   客户列表    True    1 
    2   添加客户    False   Null(不会被渲染信息管理里没有)
    3   编辑客户    False   Null 
    4   删除客户    False   Null 
    
    
    

1.1 Design table structure

models.py

class Menu(models.Model):   一级标题    

    title  = m.C('一级菜单')    
    icon   = m.C('1图标')     

class Permission(m.M):  
    title url   
    menu = m.Foreignkey('Menu',Null = True) #默认为null 自动加id  
    

Add menu database:

Add Permission Database:

1.2 Value

permission_input.py

permissions =   models.P.o.f(r_u_n=user_obj).value('title','url','pk','menu_id','menu_pk','menu_icon','menu_title','menu_pk','menu_pid').distinct()
之前是object对象   现在是字典     

1.3 Design Data Structure

‘目标数据结构’        
{1:{# 1 --menu_id       
'title':'..',       
'icon':'xxx',       
children:[{'title':'二级',‘url’:'xxx'},       
{'title':'二级',‘url’:'xxx'},]    
},      
2:{# 2 --menu_id    
'title':'..',   
'icon':'xxx',   
children:[{'title':'二级',‘url’:'xxx'},   
{'title':'二级',‘url’:'xxx'},]    
},  
}   
permission_input.py
permission_list = []
permission_menu_dict = {}
for item in Permissions : per_list.append(item['url'])
if item['menu_id'] :
    if item['menu_id'] in permission_menu_dict:
            per_men_dict[item['menu__pk']]['children'].append( [
     ['title':item['title'],'url':item['url'], ]
 )
    else:
    per_men_dict[item['menu__pk']] = {
    'title':item['menu__title'],
    'icon':item['menu__icon'],
    'children':[
    {['title':item['title'],
    'url':item['url'],]}
    ]
} 



rbac.py 取值
@incions_flag
menu_dict = requset.session.get('per_menu_dict')
for key,item in menu_dict.item():
    item['class'] = 'hide'
    for child in item['children']:
        
        if re.match('^{}$'.format(child['url']['url'],request.paht)):       
            item['class'] = ''          #给父级标签+去隐藏
            #item['class'] = 'active'
            child['class'] = 'active'
            break



menu.html   
{% for item in menu_dict.values
div class = 'x1'
    div class = 'x2 hide'           #二级下拉隐藏了 
 
            <i class={{item.icon}} i> {{item.title}}
        for child in item.children
        div class = 'body' {{item.class}}
        a href {{child.url   class='{{child.class}}' {{ch.t}}
        

1.4 left of the url and add url such as conflict

Add edit points when the left has hidden

(How to solve the other methods)

One kind of resolve:

Because the same pages, are beginning coustomer, to correct the

Simple, but can only write dead

Two kinds resolved:

Edit Delete permission to add into the table

permission 
id      url                 title       pid
1       /customer/list/     查看客户    null
2       /customer/add/      查看客户    1   
3       /customer/edit/     查看客户    1   
4       /customer/delete/       查看客户    1   
    和我的当下pid相同  默认加
    

models.py
pid = models.ForeignKey('self',null=True)   #
        + null  1    1    1     + null  5   5   5
permission_input.py
子:append({'pk':item['pk']})
父:append({'pk':item['pk']})
1 设置数据库,添加数据
2 当前请求的pid == id      1  111       5   555
rbac.py
if request.pid == child['pk']:   有分类的 null  1  
中间件的时候把pid放进去   
per_input.py
for itme in permissions:
    per__list.append('url':item['url'],'pid':item['pid'])

Middleware

for item in re.se['per_list']:
​   reg = item['url']
    ret = re.sea
    if ret:
        request.show_id = item['pid']
        return None     
        

In per_input.py

append('pk':itme['pk'])     

3 ways:

pid set to 11,115,555 no sense of belonging. . .

rbac.py

if request.show_id == child['pk']:
    item['class'] = ''   child='active'

! Permission ownership, slave and my id is pid] (E: \ Download \ qq_download \ teacher_huatu_download \ s20py \ Django \ day75 \ permission ownership, slave and my id is pid.png)

补充:
    菜单{url://,title:'客户','pk':1}
        {url://,title:'财务','pk':5}
    二级菜单:
        点的时候,还显示

When the function really can not add, change table structure, plus the flag

1.5 Permissions button

customer_list.html
#{% if '/customer/add/' in request.session.permission_list%}
        
{%load rbac}    
{% if '/customer/add/'|haspermission:request%}#这里的url太长了,在给他写个别名    {% if url 'customer'} 不行?!
        
{%endif%}
{% if '/customer/edit'+id+'/'|haspermission:request}

{% href = '/customer/edit/'{{row.id}}/}

html里的
if 过滤器没法传参 
正常不是过滤器的时候可以传参  {%}
就随便写个值 
{% if '/customer/edit/1'|haspermission:request%} 后端/customer/edit/(\d+)/ 匹配出来可以 有权限
路径太长了 用别名
选项 if edit or delete|has : 显示  没有的话,不显示选项
            一个删除或者编辑有的话,显示选项td
只是显示的,不是真的删除。/customer/edit/5/再另写   

templatags : rbac.py
from django import reverse
@register.filter
def haspermission(base_url,request):
    base_url = reverse(base_url)    #弄成别名
    for item in reqeust.session['per_list']:
        reg = '^%s$' % item['url']
        ret = re.search(reg,request.path)
        if ret:
            return True
    return False
    
    
    
    2 版  给传一个url  
    if item['name'] == base_url:
        return True
    
    
urls.py
url(name='customer')
url(name='customeradd')



customer.py
customer_list(request):
    print(reverse('customeredit',args=(1,)))    >>>>>>>>>/customer/edit/1/
(1) a menu:
1 数据库表结构:权限与用户之间的关系 rbac
2 用户登陆  登陆成功 initialsession 把权限列表和菜单列表注入session     
permissions = modes.P.o.f(r_u_n=user_obj).distinct()
for item in p: p.app(item.url)  
    if item.is_menu:per_m_l.append('title':item.titl)
3 (写死)在单独函数
incluseion_tag: 循环遍历菜单列表P_m_l:  if is_menu:
4  
(2) dry interface, the interaction front and rear ends
最多的就是设计表结构,数据结构
写后端挺简单的 前端挺辛苦的
(3) database - meaning

delete

(4) Interview Q
整个流程
rbac 的流程
怎么设计的权限表
二级菜单怎么设计的
(5) to emphasize a point render (alias)

return(request,'customer_list.html',{'date_list':date_list})

String replacement

customer_list.html

script   
$.ajax({
url:‘{%url 'customer'}}’   #{%url 'customer'}} 觉得是一个变量  undefined 
​                           # 这样是路径
type:'post',
data:{},)

Put the js file

script src = '{% static 'js/test.js'%}'
render成了路径 script src = 'test.js'
js里     url:‘{%url 'customer'}}’    的是字符串不认识        
    
    1 写死 : url:'/customer/list'
    2 写了里面

2 af_prac

(6) a function of reference

不是 from app01 import permission_input

Correct answer:

from app01.permission_input import intitial_permission

Lesson 3 pm

drf django_rest-framework

Instead of the engine to render django, the framework is to render

luffycity.com www.apeland.cn

Code Specification

After a big project folder to put views in writing multiple files decoupling function

Guide package:

The standard library (not allowed to pip install) third-party libraries (pip install) own package (custom)

from django.shortcuts import render

from django.shortcuts import reverse

from django.shortcuts import (render,reverse)

debug

Debugger down the implementation process frames from the ram at the break

比print好 , 固定的变量    多个变量

Debugging is too slow: console -> Enter the username direct access to

err_msg no error will not print variables, operation codes may also be layers: models.User.objects.filter (name = username, pwd = pwd) -> <QuerySet []>

Look source habits: in front of the source code are written down Debugger -> Frames are recorded in the unlikely love to try hard, try, change perception

Hit two points and make a point What is the difference:

Point two: intermediate code directly go | "

specification

Spaces between two variables

A py program a space blank line to indicate the end of the code

The yellow line means no call to

Simple code to put up, avoid excessive levels

More specifications pep8

# TODO 我做了一件什么事情 没做完? 记录
# FIXME  这里没有做极值的判断,可能会出现bug
signal

Send information to promote

What information is sent to a certain class

return redirect(reverse('customer'))

Created, go to treatment

The data is correct, (and he did not feel right front-end written) front-end display is not right, because there is no introduction js and css

js not directly reference template again, you have to lead in the extends js

menu.css

.multi-menu .item > .body a.active {
    border-left: 2px solid #2F72AB;
}

menu.js

$('.item .title').click(function () {
    $(this).next().toggleClass('hide');     点一下一级菜单张合二级菜单       第一下二级菜单不变因为没有下级
    $(this).parent().siblings().children(".body").addClass("hide")      点一下一级菜单 本菜单的兄弟的子代添加隐藏
});

menu.py

for key,item in menu_dict.items():
    item['class'] = 'hide'
    for child in item['children']:
        if request.show_id == child['pk']:  #bug 第一次来时,就有下拉了。。。
            item['class'] = ''      #作用是一直保持‘’ 不隐藏  前端实现了动态 
            child['class'] = 'active'   #加了状态  巩固下拉菜单的
            break
return {"menu_dict": menu_dict}

menu.html

{% for item in menu_dict.values %}
    <div class="item">
    <div class="title"> <i class="{{ item.icon }}"></i>{{ item.title }}</div>
        {% for child in item.children %}
        <div class="body {{ item.class }}">
      <a href="{{ child.url }}" class="{{ child.class }}">{{ child.title }}</a>            </div>
        {% endfor %}
    </div>
{% endfor %}

Solution: Building brother asked: I say a little js file

Asked Po brother and brother bright, clear about the cache, there was the normal format

When (8) customer determination button Add

With / customer / edit / 1 match / customer / edit / (\ d +) display

Write Filter

The role of simple good multiplexing, less code

@register.filter
def haspermission(base_url,request):
    for item in req.ses.get('perssion_list'):
        reg=item['url']
        if re.match('^%s$'.format(reg),base_url):
            return True
    return False

Reproduced in: https: //www.cnblogs.com/Doner/p/11044194.html

Guess you like

Origin blog.csdn.net/weixin_30642561/article/details/95303276
75
75
75