Django开发之html交互

html中用户输入信息,由Django的view.py处理,大致用到了以下几类格式:

1. 文本框

<input type="text" name="vid" size="10" height="20">
或由bootcss修饰的
<div class="col-sm-2" >
<input type="text" class="form-control" id="username" name="username" placeholder="Username">
</div>

  

这种相对比较简单

version_num=request.GET.get("vid")

  

2. 文本框

<td width="30%" align="center" valign="top">粘帖需要发布的其他所有UI</td>
<td><textarea rows="10" cols="40" id="newui" name="newui" ></textarea></td>

 这种要这样接收,返回结果是一个列表,相对比较好处理

    otherNewUI=request.GET.get("newui").encode(utf8).split("\r\n")
    ‘‘‘return result like this: 
    [u‘GameStub.swf.new‘, u‘giftui.swf‘, u‘zhuui2.swf‘]
    ‘‘‘

  

3. 多选按钮,返回值也是一个列表

        <td width="30%" align="center" valign="top" >选择ini.xml中需要发布的ui</td>
        <td>
            <input type="checkbox" name="ui_list" value="GameStub.swf.new" />GameStub.swf.new<br />
            <input type="checkbox" name="ui_list" value="giftui.swf" />giftui.swf<br />
            <input type="checkbox" name="ui_list" value="zhuui2.swf" />zhuui2.swf<br />
            <input type="checkbox" name="ui_list" value="vipui.swf" />vipui.swf<br />
            <input type="checkbox" name="ui_list" value="giftboxui.swf" />giftboxui.swf<br />
            <input type="checkbox" name="ui_list" value="baseui.swf" />baseui.swf<br />
            <input type="checkbox" name="ui_list" value="xinshouui.swf" />xinshouui.swf<br />
            <input type="checkbox" name="ui_list" value="buildingui.swf" />buildingui.swf<br />
            <input type="checkbox" name="ui_list" value="jiuguanui.swf" />jiuguanui.swf<br />
        </td>
    newUiList=request.GET.getlist("ui_list")
    ‘‘‘return result like this: 
    [u‘GameStub.swf.new‘, u‘giftui.swf‘, u‘zhuui2.swf‘]
    ‘‘‘

  

4. cookie用法

写cookie,并执行跳转
    response=render_to_response(verIntegration/uiPrepare.html,{"current_version":current_version,"gameName":gameName})
    response.set_cookie("game",game,600)
    return response

读cookie,判断cookie是否失效
    if "game" in request.COOKIES:
        game=request.COOKIES["game"]
        gameName=CONFIG[game][name]
        print "cookie still in "
    else:
        gameName="未知"
        print "cookie has gone"
        return render_to_response(verIntegration/uiPrepare.html,{"gameName":gameName})

  

5. pexpect方法执行交互式操作

def pullFile(gameConfig,remoteFileName,localFileName):
    #使用目录均为相对路径,绝对路径从字典中读取
    cmd="scp -P %s %s@%s:%s/%s %s/%s" % (gameConfig[port],gameConfig[user],gameConfig[ip],gameConfig[remote_dir],remoteFileName,gameConfig[local_dir],localFileName)
    expect1="password: "
    child = pexpect.spawn(cmd)
    child.expect(expect1)
    child.sendline(gameConfig[password])
    child.read()
    return None

  

6. pexpect远程执行脚本,这个可以参考pexpect的example中的hive.py

def remoteRun(gameConfig,cmd):
    srv_ip=gameConfig[ip]
    srv_port=gameConfig[port]
    srv_username=gameConfig[user]
    srv_password=gameConfig[password]
    try:
        ssh=paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(srv_ip,srv_port,srv_username,srv_password,timeout=5)
        stdin, stdout, stderr = ssh.exec_command(cmd)
        
        outlist=[]
        for out in stdout:
            outlist.append(out.strip(\n))
    except Exception,e:
        print "Error"
        print e
    return None

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。