django将数组传递给前台模板的方法

yipeiwu_com5年前Python基础

将数组传递给前台模板:

1.

def modifyBtn(req,modifyip):
  print modifyip
  conn= MySQLdb.connect(
    host='localhost',
    port = 3306,
    user='root',
    passwd='1234567',
    db ='DEVOPS'
    )
  cur = conn.cursor()
  a = cur.execute("select ip,info,env from machine_info where ip=%s ",[modifyip])
  info = cur.fetchall()
  print info
  print type(info)
  return render(req,'cmdb/modifyBtn.html',{'info':info})

2. node2:/django/mysite/news/templates/cmdb#cat modifyBtn.html

<html>
<head>
 <title>运维平台</title>
 <link rel="stylesheet" type="text/css" href="/static/Css/Equipment/modifyBtn.css" rel="external nofollow" >
 <link rel="stylesheet" type="text/css" href="/static/Css/Public/header.css" rel="external nofollow" >
 <link rel="stylesheet" type="text/css" href="/static/Css/Public/menu.css" rel="external nofollow" >
</head>
<body>
 <include file="Public:header"/>
 <div class="content">
 <include file="Public:menu"/>
 <div class="con fl">
  <form id="condition" action="/static/modifyEquipment" method="post">
                {% for x in info %}
  <label class="condition">地址</label><input type="text" name="ip" class="equipment_sz" value={{x.0}}>
  <label class="condition">描述</label><input type="text" name="info" class="equipment_sz" value={{x.1}}>
  <label class="condition">环境</label><input type="text" name="env" class="equipment_sz" value={{x.2}}>
  <input type="submit" value="设备修改" class="equipment_add_btn">
                {% endfor %}
  </form>
 </div>
 </div>
</body>
<script type="text/javascript" src="/static/Js/jquery-2.2.2.min.js"></script>
<!-- <script type="text/javascript" src="/static/Js/Equipment/addEquipment.js"></script> -->
</html>

以上这篇django将数组传递给前台模板的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

详解Python装饰器

1. 定义 本质是函数,用来装饰其他函数,为其他函数添加附加功能 2. 原则 a. 不能修改被装饰函数的源代码 b. 不能修改被装饰的函数的调用方式 3. 实现装饰器知识储备 a. 函数...

使用py2exe在Windows下将Python程序转为exe文件

前提条件: 需要安装easy-install模块,这是一个python的模块打包工具。 首先下载easy_setup.py的源代码,下载地址: http://pypi.python.o...

Python读取excel中的图片完美解决方法

Python读取excel中的图片完美解决方法

excel中有图片是很常见的,但是通过python读取excel中的图片没有很好的解决办法。 网上找了一种很聪明的方法,原理是这样的: 1、将待读取的excel文件后缀名改成zip,变成...

Python随机生成均匀分布在单位圆内的点代码示例

Python随机生成均匀分布在单位圆内的点代码示例

Python有一随机函数可以产生[0,1)区间内的随机数,但是如果我们想生成随机分布在单位圆上的,那么我们可以首先生成随机分布在单位圆边上的点,然后随机调整每个点距离原点的距离,但是我们...

对python_discover方法遍历所有执行的用例详解

对python_discover方法遍历所有执行的用例详解

当我们写了一个单个py的测试文件时直接运行就ok了,但当我们有很多很多个这样的py时,难道要一个一个的点击来运行吗,当然不是。我们可以通过discover方法来找到所有的用例。 下面直接...