学习了别人的运维平台的代码,对其中的一段代码做了小修改

学习了静以修身养心的运维平台开发的代码,对views.py做了下小修改

原文:http://blog.csdn.net/alex_chen_16/article/details/50850435

def lists(request, table):  
    #从根据不同的请求,来获取相应的数据,并跳转至相应页面  
    lists = {'node': Node, 'line': Line, 'device': Device}
    list_template = {'node': 'node_list.html', 
    'line': 'line_list.html', 'device': 'device_list.html'}

    if table in ['node', 'line', 'device']:
        data = lists[table].object.all()
    #建立context字典,将值传递到相应页面  
    context = {  
        'data': data,  
    }  
    #跳转到相应页面,并将值传递过去  
    return render(request,list_template[table],context)  


def add(request, table):   
    #根据提交的请求不同,获取来自不同Form的表单数据  
    tables = {'node': NodeForm, 'line': LineForm, 'device': DeviceForm}
    signers = {'node': node_signer, 'line': line_signer, 'device': device_signer}

    if request.method == 'POST':
        form = tables[table](request.POST)  
    #判断form是否有效  
        if form.is_valid():  
        #创建实例,需要做些数据处理,暂不做保存  
             instance = form.save(commit=False)  
        #将登录用户作为登记人  
            instance.signers[table] = request.user   
        #保存该实例  
            instance.save()  
        #跳转至列表页面,配合table参数,进行URL的反向解析  
        return redirect('lists', table=table)
    else:
        form = tables[table]()
  
    #创建context来集中处理需要传递到页面的数据  
    context = {  
        'form': form,  
    }  
    #如果没有有效提交,则仍留在原来页面  
    return render(request, 'add.html',  context)  


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值