简体   繁体   English

Django - 将变量从函数传递到另一个函数

[英]Django - Passing variables from a function to another

I need some of your help please, 我需要你的一些帮助,

I'm working with pysftp this is working great but now I'm trying to make it work to my project in Django worked great in console but I want to get the data from a form so I won't need to use the console to do that. 我正在使用pysftp这工作得很好,但现在我正在努力让它在我的项目中工作在Django在控制台工作得很好但我想从form获取数据所以我不需要使用console来去做。

here's my view: 这是我的观点:

def sftp_form(request):
    if request.method == 'POST':
        form = sftpForm(request.POST or None)
        if form.is_valid():
            data = form.cleaned_data
            host = data['host']
            usuario = data['usuario']
            clave = data['clave']
            print host
            print usuario
            print clave
    else:
        form=sftpForm()
    return render(request, 'sftp.html', {'form':form})


def SFTP_subir():
    host = raw_input('ingrese el host: ') # I want form's host here.
    usuario = raw_input('ingrese el usuario: ')# I want form's usuario here.
    clave = raw_input('ingrese la clave: ')# I want form's clave here.
    try:
        transferencia = sftp.Connection(host=host, username=usuario, password=clave)

        remotepath= 'remotepath'
        localpath="mylocalpath"

        transferencia.put(localpath,remotepath)

        print ('\n' + 'Sucess.')

    except Exception, e:
        print str(e)

as you can see in my code sftp_subir() it's asking me for host,usuario and clave from console, but I want to make it work with sftp_form() host,usuario and clave. 正如你在我的代码sftp_subir()看到的那样,它要求我提供主机,usuario和clave来自控制台,但我想让它与sftp_form()主机,usuario和clave一起使用。

There seem to be a slight mixup here, you can't use raw_input in a django web app. 这里似乎有轻微的混淆,你不能在django网络应用程序中使用raw_input If you using Django as a CLI you can't use an HTTP request. 如果您使用Django作为CLI ,则无法使用HTTP请求。 As @sayse suggested in the comments, if you are using a view in a web app all you need to do is to define your second function to be one that accepts paramers 正如@sayse在评论中建议的那样,如果您在Web应用程序中使用视图,那么您需要做的就是将第二个函数定义为接受参数的函数

def sftp_form(request):
    if request.method == 'POST':
        form = sftpForm(request.POST or None)
        if form.is_valid():
            data = form.cleaned_data
            host = data['host']
            usuario = data['usuario']
            clave = data['clave']
            SFTP_subir(hosts, usuario,clave)

    else:
        form=sftpForm()
    return render(request, 'sftp.html', {'form':form})


def SFTP_subir(hosts, usuario,clave):
    try:
        transferencia = sftp.Connection(host=host, username=usuario, password=clave)

        remotepath= 'remotepath'
        localpath="mylocalpath"

        transferencia.put(localpath,remotepath)

        print ('\n' + 'Sucess.')

    except Exception, e:
        print str(e)

Once you make this code you still have a long way to go because your SFTP method doesn't return any usefull response. 一旦你创建了这段代码,你还有很长的路要走,因为你的SFTP方法没有返回任何有用的响应。

In your view: 在你看来:

def sftp_form(request):
    if request.method == 'POST':
        form = sftpForm(request.POST or None)
        if form.is_valid():
            data = form.cleaned_data
            host = data['host']
            usuario = data['usuario']
            clave = data['clave']
            print host
            print usuario
            print clave
            SFTP_subir(host, usuario, clave) # here you invoke the function, passing variables as arguments 
    else:
        form=sftpForm()
    return render(request, 'sftp.html', {'form':form})

Then refactor your function to receive those params: 然后重构你的函数来接收这些参数:

def SFTP_subir(host, usuario, clave):
    try:
        transferencia = sftp.Connection(host=host, username=usuario, password=clave)

        remotepath= 'remotepath'
        localpath="mylocalpath"

        transferencia.put(localpath,remotepath)

        print ('\n' + 'Sucess.')

    except Exception, e:
        print str(e)

you can do the 你可以做到

sftp.connect(...)
...
<4 lines following>

inside the request.method == "POST" block instead of your print statements. 在request.method ==“POST”块内,而不是你的print语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM